@@ -6433,4 +6433,109 @@ function (mixed $value) { return str_replace('prefix_', '', $value); }
64336433
64346434 $ database ->deleteCollection ($ collectionId );
64356435 }
6436+
6437+ public function testUpdateDocumentsSuccessiveCallsDoNotResetDefaults (): void
6438+ {
6439+ /** @var Database $database */
6440+ $ database = static ::getDatabase ();
6441+
6442+ if (!$ database ->getAdapter ()->getSupportForBatchOperations ()) {
6443+ $ this ->expectNotToPerformAssertions ();
6444+ return ;
6445+ }
6446+
6447+ $ collectionId = 'successive_updates ' ;
6448+ Authorization::cleanRoles ();
6449+ Authorization::setRole (Role::any ()->toString ());
6450+
6451+ // Create collection with two attributes that have default values
6452+ $ database ->createCollection ($ collectionId );
6453+ $ database ->createAttribute ($ collectionId , 'attrA ' , Database::VAR_STRING , 50 , false , 'defaultA ' );
6454+ $ database ->createAttribute ($ collectionId , 'attrB ' , Database::VAR_STRING , 50 , false , 'defaultB ' );
6455+
6456+ // Create a document without setting attrA or attrB (should use defaults)
6457+ $ doc = $ database ->createDocument ($ collectionId , new Document ([
6458+ '$id ' => 'testdoc ' ,
6459+ '$permissions ' => [
6460+ Permission::read (Role::any ()),
6461+ Permission::update (Role::any ()),
6462+ ],
6463+ ]));
6464+
6465+ // Verify initial defaults
6466+ $ this ->assertEquals ('defaultA ' , $ doc ->getAttribute ('attrA ' ));
6467+ $ this ->assertEquals ('defaultB ' , $ doc ->getAttribute ('attrB ' ));
6468+
6469+ // First update: set attrA to a new value
6470+ $ count = $ database ->updateDocuments ($ collectionId , new Document ([
6471+ 'attrA ' => 'updatedA ' ,
6472+ ]));
6473+ $ this ->assertEquals (1 , $ count );
6474+
6475+ // Verify attrA was updated
6476+ $ doc = $ database ->getDocument ($ collectionId , 'testdoc ' );
6477+ $ this ->assertEquals ('updatedA ' , $ doc ->getAttribute ('attrA ' ));
6478+ $ this ->assertEquals ('defaultB ' , $ doc ->getAttribute ('attrB ' ));
6479+
6480+ // Second update: set attrB to a new value
6481+ $ count = $ database ->updateDocuments ($ collectionId , new Document ([
6482+ 'attrB ' => 'updatedB ' ,
6483+ ]));
6484+ $ this ->assertEquals (1 , $ count );
6485+
6486+ // Verify attrB was updated AND attrA is still 'updatedA' (not reset to 'defaultA')
6487+ $ doc = $ database ->getDocument ($ collectionId , 'testdoc ' );
6488+ $ this ->assertEquals ('updatedA ' , $ doc ->getAttribute ('attrA ' ), 'attrA should not be reset to default ' );
6489+ $ this ->assertEquals ('updatedB ' , $ doc ->getAttribute ('attrB ' ));
6490+
6491+ $ database ->deleteCollection ($ collectionId );
6492+ }
6493+
6494+ public function testUpdateDocumentSuccessiveCallsDoNotResetDefaults (): void
6495+ {
6496+ /** @var Database $database */
6497+ $ database = static ::getDatabase ();
6498+
6499+ $ collectionId = 'successive_update_single ' ;
6500+ Authorization::cleanRoles ();
6501+ Authorization::setRole (Role::any ()->toString ());
6502+
6503+ // Create collection with two attributes that have default values
6504+ $ database ->createCollection ($ collectionId );
6505+ $ database ->createAttribute ($ collectionId , 'attrA ' , Database::VAR_STRING , 50 , false , 'defaultA ' );
6506+ $ database ->createAttribute ($ collectionId , 'attrB ' , Database::VAR_STRING , 50 , false , 'defaultB ' );
6507+
6508+ // Create a document without setting attrA or attrB (should use defaults)
6509+ $ doc = $ database ->createDocument ($ collectionId , new Document ([
6510+ '$id ' => 'testdoc ' ,
6511+ '$permissions ' => [
6512+ Permission::read (Role::any ()),
6513+ Permission::update (Role::any ()),
6514+ ],
6515+ ]));
6516+
6517+ // Verify initial defaults
6518+ $ this ->assertEquals ('defaultA ' , $ doc ->getAttribute ('attrA ' ));
6519+ $ this ->assertEquals ('defaultB ' , $ doc ->getAttribute ('attrB ' ));
6520+
6521+ // First update: set attrA to a new value
6522+ $ doc = $ database ->updateDocument ($ collectionId , 'testdoc ' , new Document ([
6523+ '$id ' => 'testdoc ' ,
6524+ 'attrA ' => 'updatedA ' ,
6525+ ]));
6526+ $ this ->assertEquals ('updatedA ' , $ doc ->getAttribute ('attrA ' ));
6527+ $ this ->assertEquals ('defaultB ' , $ doc ->getAttribute ('attrB ' ));
6528+
6529+ // Second update: set attrB to a new value
6530+ $ doc = $ database ->updateDocument ($ collectionId , 'testdoc ' , new Document ([
6531+ '$id ' => 'testdoc ' ,
6532+ 'attrB ' => 'updatedB ' ,
6533+ ]));
6534+
6535+ // Verify attrB was updated AND attrA is still 'updatedA' (not reset to 'defaultA')
6536+ $ this ->assertEquals ('updatedA ' , $ doc ->getAttribute ('attrA ' ), 'attrA should not be reset to default ' );
6537+ $ this ->assertEquals ('updatedB ' , $ doc ->getAttribute ('attrB ' ));
6538+
6539+ $ database ->deleteCollection ($ collectionId );
6540+ }
64366541}
0 commit comments