@@ -61,7 +61,8 @@ public function testFindActsAsGetWithNoParams()
6161 {
6262 $ model = new JobModel ($ this ->db );
6363
64- $ jobs = $ model ->asArray ()->find ();
64+ $ jobs = $ model ->asArray ()
65+ ->find ();
6566
6667 $ this ->assertCount (4 , $ jobs );
6768
@@ -78,7 +79,8 @@ public function testFindRespectsReturnArray()
7879 {
7980 $ model = new JobModel ($ this ->db );
8081
81- $ job = $ model ->asArray ()->find (4 );
82+ $ job = $ model ->asArray ()
83+ ->find (4 );
8284
8385 $ this ->assertInternalType ('array ' , $ job );
8486 }
@@ -89,7 +91,8 @@ public function testFindRespectsReturnObject()
8991 {
9092 $ model = new JobModel ($ this ->db );
9193
92- $ job = $ model ->asObject ()->find (4 );
94+ $ job = $ model ->asObject ()
95+ ->find (4 );
9396
9497 $ this ->assertInternalType ('object ' , $ job );
9598 }
@@ -98,15 +101,19 @@ public function testFindRespectsReturnObject()
98101
99102 public function testFindRespectsSoftDeletes ()
100103 {
101- $ this ->db ->table ('user ' )->where ('id ' , 4 )->update (['deleted ' => 1 ]);
104+ $ this ->db ->table ('user ' )
105+ ->where ('id ' , 4 )
106+ ->update (['deleted ' => 1 ]);
102107
103108 $ model = new UserModel ($ this ->db );
104109
105- $ user = $ model ->asObject ()->find (4 );
110+ $ user = $ model ->asObject ()
111+ ->find (4 );
106112
107113 $ this ->assertEmpty ($ user );
108114
109- $ user = $ model ->withDeleted ()->find (4 );
115+ $ user = $ model ->withDeleted ()
116+ ->find (4 );
110117
111118 // fix for PHP7.2
112119 $ count = is_array ($ user ) ? count ($ user ) : 1 ;
@@ -123,7 +130,8 @@ public function testFindClearsBinds()
123130 $ model ->find (1 );
124131
125132 // Binds should be reset to 0 after each one
126- $ binds = $ model ->builder ()->getBinds ();
133+ $ binds = $ model ->builder ()
134+ ->getBinds ();
127135 $ this ->assertCount (0 , $ binds );
128136
129137 $ query = $ model ->getLastQuery ();
@@ -167,15 +175,18 @@ public function testFindAllRespectsLimitsAndOffset()
167175
168176 public function testFindAllRespectsSoftDeletes ()
169177 {
170- $ this ->db ->table ('user ' )->where ('id ' , 4 )->update (['deleted ' => 1 ]);
178+ $ this ->db ->table ('user ' )
179+ ->where ('id ' , 4 )
180+ ->update (['deleted ' => 1 ]);
171181
172182 $ model = new UserModel ($ this ->db );
173183
174184 $ user = $ model ->findAll ();
175185
176186 $ this ->assertCount (3 , $ user );
177187
178- $ user = $ model ->withDeleted ()->findAll ();
188+ $ user = $ model ->withDeleted ()
189+ ->findAll ();
179190
180191 $ this ->assertCount (4 , $ user );
181192 }
@@ -186,7 +197,8 @@ public function testFirst()
186197 {
187198 $ model = new UserModel ();
188199
189- $ user = $ model ->where ('id > ' , 2 )->first ();
200+ $ user = $ model ->where ('id > ' , 2 )
201+ ->first ();
190202
191203 // fix for PHP7.2
192204 $ count = is_array ($ user ) ? count ($ user ) : 1 ;
@@ -198,7 +210,9 @@ public function testFirst()
198210
199211 public function testFirstRespectsSoftDeletes ()
200212 {
201- $ this ->db ->table ('user ' )->where ('id ' , 1 )->update (['deleted ' => 1 ]);
213+ $ this ->db ->table ('user ' )
214+ ->where ('id ' , 1 )
215+ ->update (['deleted ' => 1 ]);
202216
203217 $ model = new UserModel ();
204218
@@ -209,7 +223,8 @@ public function testFirstRespectsSoftDeletes()
209223 $ this ->assertEquals (1 , $ count );
210224 $ this ->assertEquals (2 , $ user ->id );
211225
212- $ user = $ model ->withDeleted ()->first ();
226+ $ user = $ model ->withDeleted ()
227+ ->first ();
213228
214229 $ this ->assertEquals (1 , $ user ->id );
215230 }
@@ -218,14 +233,16 @@ public function testFirstWithNoPrimaryKey()
218233 {
219234 $ model = new SecondaryModel ();
220235
221- $ this ->db ->table ('secondary ' )->insert ([
222- 'key ' => 'foo ' ,
223- 'value ' => 'bar ' ,
224- ]);
225- $ this ->db ->table ('secondary ' )->insert ([
226- 'key ' => 'bar ' ,
227- 'value ' => 'baz ' ,
228- ]);
236+ $ this ->db ->table ('secondary ' )
237+ ->insert ([
238+ 'key ' => 'foo ' ,
239+ 'value ' => 'bar ' ,
240+ ]);
241+ $ this ->db ->table ('secondary ' )
242+ ->insert ([
243+ 'key ' => 'bar ' ,
244+ 'value ' => 'baz ' ,
245+ ]);
229246
230247 $ record = $ model ->first ();
231248
@@ -243,7 +260,8 @@ public function testSaveNewRecordObject()
243260 $ data ->name = 'Magician ' ;
244261 $ data ->description = 'Makes peoples things dissappear. ' ;
245262
246- $ model ->protect (false )->save ($ data );
263+ $ model ->protect (false )
264+ ->save ($ data );
247265
248266 $ this ->seeInDatabase ('job ' , ['name ' => 'Magician ' ]);
249267 }
@@ -259,7 +277,8 @@ public function testSaveNewRecordArray()
259277 'description ' => 'That thing you do. ' ,
260278 ];
261279
262- $ result = $ model ->protect (false )->save ($ data );
280+ $ result = $ model ->protect (false )
281+ ->save ($ data );
263282
264283 $ this ->seeInDatabase ('job ' , ['name ' => 'Apprentice ' ]);
265284 }
@@ -276,7 +295,8 @@ public function testSaveUpdateRecordObject()
276295 'description ' => 'That thing you do. ' ,
277296 ];
278297
279- $ result = $ model ->protect (false )->save ($ data );
298+ $ result = $ model ->protect (false )
299+ ->save ($ data );
280300
281301 $ this ->seeInDatabase ('job ' , ['name ' => 'Apprentice ' ]);
282302 $ this ->assertTrue ($ result );
@@ -293,7 +313,8 @@ public function testSaveUpdateRecordArray()
293313 $ data ->name = 'Engineer ' ;
294314 $ data ->description = 'A fancier term for Developer. ' ;
295315
296- $ result = $ model ->protect (false )->save ($ data );
316+ $ result = $ model ->protect (false )
317+ ->save ($ data );
297318
298319 $ this ->seeInDatabase ('job ' , ['name ' => 'Engineer ' ]);
299320 $ this ->assertTrue ($ result );
@@ -311,7 +332,8 @@ public function testSaveProtected()
311332 $ data ->description = 'A fancier term for Developer. ' ;
312333 $ data ->random_thing = 'Something wicked ' ; // If not protected, this would kill the script.
313334
314- $ result = $ model ->protect (true )->save ($ data );
335+ $ result = $ model ->protect (true )
336+ ->save ($ data );
315337
316338 $ this ->assertTrue ($ result );
317339 }
@@ -379,7 +401,8 @@ public function testDeleteNoParams()
379401
380402 $ this ->seeInDatabase ('job ' , ['name ' => 'Developer ' ]);
381403
382- $ model ->where ('id ' , 1 )->delete ();
404+ $ model ->where ('id ' , 1 )
405+ ->delete ();
383406
384407 $ this ->dontSeeInDatabase ('job ' , ['name ' => 'Developer ' ]);
385408 }
@@ -390,11 +413,14 @@ public function testPurgeDeleted()
390413 {
391414 $ model = new UserModel ();
392415
393- $ this ->db ->table ('user ' )->where ('id ' , 1 )->update (['deleted ' => 1 ]);
416+ $ this ->db ->table ('user ' )
417+ ->where ('id ' , 1 )
418+ ->update (['deleted ' => 1 ]);
394419
395420 $ model ->purgeDeleted ();
396421
397- $ users = $ model ->withDeleted ()->findAll ();
422+ $ users = $ model ->withDeleted ()
423+ ->findAll ();
398424
399425 $ this ->assertCount (3 , $ users );
400426 }
@@ -405,9 +431,12 @@ public function testOnlyDeleted()
405431 {
406432 $ model = new UserModel ($ this ->db );
407433
408- $ this ->db ->table ('user ' )->where ('id ' , 1 )->update (['deleted ' => 1 ]);
434+ $ this ->db ->table ('user ' )
435+ ->where ('id ' , 1 )
436+ ->update (['deleted ' => 1 ]);
409437
410- $ users = $ model ->onlyDeleted ()->findAll ();
438+ $ users = $ model ->onlyDeleted ()
439+ ->findAll ();
411440
412441 $ this ->assertCount (1 , $ users );
413442 }
@@ -434,6 +463,7 @@ public function testValidationBasics()
434463 $ model = new ValidModel ($ this ->db );
435464
436465 $ data = [
466+ 'name ' => null ,
437467 'description ' => 'some great marketing stuff ' ,
438468 ];
439469
@@ -481,7 +511,21 @@ public function testSkipValidation()
481511 'description ' => 'some great marketing stuff ' ,
482512 ];
483513
484- $ this ->assertInternalType ('numeric ' , $ model ->skipValidation (true )->insert ($ data ));
514+ $ this ->assertInternalType ('numeric ' , $ model ->skipValidation (true )
515+ ->insert ($ data ));
516+ }
517+
518+ public function testValidationSkipsNonExistingFields ()
519+ {
520+ $ model = new ValidModel ($ this ->db );
521+
522+ $ data = [
523+ 'description ' => 'some great marketing stuff ' ,
524+ 'id ' => 42 ,
525+ 'token ' => 42 ,
526+ ];
527+
528+ $ this ->assertEquals (5 , $ model ->insert ($ data ));
485529 }
486530
487531 //--------------------------------------------------------------------
@@ -490,7 +534,8 @@ public function testCanCreateAndSaveEntityClasses()
490534 {
491535 $ model = new EntityModel ($ this ->db );
492536
493- $ entity = $ model ->where ('name ' , 'Developer ' )->first ();
537+ $ entity = $ model ->where ('name ' , 'Developer ' )
538+ ->first ();
494539
495540 $ this ->assertInstanceOf (SimpleEntity::class, $ entity );
496541 $ this ->assertEquals ('Developer ' , $ entity ->name );
@@ -593,7 +638,8 @@ public function testSetWorksWithInsert()
593638 'email ' => 'foo@example.com ' ,
594639 'name ' => 'Foo Bar ' ,
595640 'country ' => 'US ' ,
596- ])->insert ();
641+ ])
642+ ->insert ();
597643
598644 $ this ->seeInDatabase ('user ' , [
599645 'email ' => 'foo@example.com ' ,
@@ -616,7 +662,8 @@ public function testSetWorksWithUpdate()
616662
617663 $ model ->set ([
618664 'name ' => 'Fred Flintstone ' ,
619- ])->update ($ userId );
665+ ])
666+ ->update ($ userId );
620667
621668 $ this ->seeInDatabase ('user ' , [
622669 'id ' => $ userId ,
@@ -643,7 +690,8 @@ public function testSetWorksWithUpdateNoId()
643690 ->where ('id ' , $ userId )
644691 ->set ([
645692 'name ' => 'Fred Flintstone ' ,
646- ])->update ();
693+ ])
694+ ->update ();
647695
648696 $ this ->seeInDatabase ('user ' , [
649697 'id ' => $ userId ,
@@ -768,7 +816,9 @@ public function testSelectAndEntitiesSaveOnlyChangedValues()
768816
769817 $ model = new EntityModel ();
770818
771- $ job = $ model ->select ('id, name ' )->where ('name ' , 'Rocket Scientist ' )->first ();
819+ $ job = $ model ->select ('id, name ' )
820+ ->where ('name ' , 'Rocket Scientist ' )
821+ ->first ();
772822
773823 $ this ->assertNull ($ job ->description );
774824 $ this ->assertEquals ('Rocket Scientist ' , $ job ->name );
@@ -786,17 +836,19 @@ public function testUpdateNoPrimaryKey()
786836 {
787837 $ model = new SecondaryModel ();
788838
789- $ this ->db ->table ('secondary ' )->insert ([
790- 'key ' => 'foo ' ,
791- 'value ' => 'bar ' ,
792- ]);
839+ $ this ->db ->table ('secondary ' )
840+ ->insert ([
841+ 'key ' => 'foo ' ,
842+ 'value ' => 'bar ' ,
843+ ]);
793844
794845 $ this ->dontSeeInDatabase ('secondary ' , [
795846 'key ' => 'bar ' ,
796847 'value ' => 'baz ' ,
797848 ]);
798849
799- $ model ->where ('key ' , 'foo ' )->update (null , ['key ' => 'bar ' , 'value ' => 'baz ' ]);
850+ $ model ->where ('key ' , 'foo ' )
851+ ->update (null , ['key ' => 'bar ' , 'value ' => 'baz ' ]);
800852
801853 $ this ->seeInDatabase ('secondary ' , [
802854 'key ' => 'bar ' ,
@@ -814,8 +866,33 @@ public function testCountAllResultsRespectsSoftDeletes()
814866 // testSeeder has 4 users....
815867 $ this ->assertEquals (4 , $ model ->countAllResults ());
816868
817- $ model ->where ('name ' , 'Derek Jones ' )->delete ();
869+ $ model ->where ('name ' , 'Derek Jones ' )
870+ ->delete ();
818871
819872 $ this ->assertEquals (3 , $ model ->countAllResults ());
820873 }
874+
875+ /**
876+ * @see https://github.com/codeigniter4/CodeIgniter4/issues/1584
877+ */
878+ public function testUpdateWithValidation ()
879+ {
880+ $ model = new ValidModel ($ this ->db );
881+
882+ $ data = [
883+ 'description ' => 'This is a first test! ' ,
884+ 'name ' => 'valid ' ,
885+ 'id ' => 42 ,
886+ 'token ' => 42 ,
887+ ];
888+
889+ $ id = $ model ->insert ($ data );
890+
891+ $ this ->assertTrue ((bool )$ id );
892+
893+ $ data ['description ' ] = 'This is a second test! ' ;
894+ unset($ data ['name ' ]);
895+
896+ $ this ->assertTrue ($ model ->update ($ id , $ data ));
897+ }
821898}
0 commit comments