@@ -18,6 +18,11 @@ class PasswordableBehaviorTest extends TestCase {
18
18
'plugin.tools.tools_users ' , 'plugin.tools.roles ' ,
19
19
];
20
20
21
+ /**
22
+ * @var \TestApp\Model\Table\ToolsUsersTable
23
+ */
24
+ public $ Users ;
25
+
21
26
/**
22
27
* SetUp method
23
28
*
@@ -391,11 +396,13 @@ public function testValidateCurrent() {
391
396
'pwd_repeat ' => '123456 '
392
397
];
393
398
$ user ->accessible ('* ' , false ); // Mark all properties as protected
394
- $ user ->accessible (['id ' , 'pwd ' , 'pwd_repeat ' , 'pwd_current ' ], true );
395
- $ this ->Users ->patchEntity ($ user , $ data );
396
- // Test whitelist setting - only "password" needs to gets auto-added
397
- $ options = ['validate ' => true , 'fieldList ' => ['id ' , 'pwd ' , 'pwd_repeat ' , 'pwd_current ' ]];
399
+ $ user ->accessible (['id ' ], true ); // Allow id to be accessible by default
400
+ $ user = $ this ->Users ->patchEntity ($ user , $ data , ['fieldList ' => ['id ' ]]);
398
401
402
+ $ this ->assertNotSame ($ is ['password ' ], $ user ['password ' ]);
403
+ $ this ->assertTrue ($ user ->dirty ('pwd ' ));
404
+
405
+ $ options = ['validate ' => true ];
399
406
$ is = $ this ->Users ->save ($ user , $ options );
400
407
$ this ->assertTrue (!empty ($ is ));
401
408
@@ -416,11 +423,11 @@ public function testValidateCurrent() {
416
423
];
417
424
$ user ->accessible ('* ' , false ); // Mark all properties as protected
418
425
$ user ->accessible (['id ' , 'name ' ], true );
419
- $ this ->Users ->patchEntity ($ user , $ data );
426
+ $ this ->Users ->patchEntity ($ user , $ data, [ ' fieldList ' => [ ' id ' , ' name ' ]] );
420
427
// Test whitelist setting - only "password" gets auto-added, pwd, pwd_repeat etc need to be added manually
421
428
// NOTE that I had to remove the code for adding those fields from the behavior (as it was not functional)
422
429
// So of course, this won't work now as expected. But feel free to try to add them in the behavior. Results will be the same.
423
- $ options = ['validate ' => true , ' fieldList ' => [ ' id ' , ' name ' ] ];
430
+ $ options = ['validate ' => true ];
424
431
$ is = $ this ->Users ->save ($ user , $ options );
425
432
426
433
// Validation errors triggered - as expected
@@ -429,17 +436,12 @@ public function testValidateCurrent() {
429
436
}
430
437
431
438
/**
432
- * Test cake2.4 passwordHasher feature
433
- *
434
439
* @return void
435
440
*/
436
- public function testPasswordHasher () {
437
- $ this ->skipIf ((float )Configure::version () < 2.4 , 'Needs 2.4 and above ' );
438
-
441
+ public function testPatchWithFieldList () {
439
442
$ this ->Users ->addBehavior ('Tools.Passwordable ' , [
440
443
'formField ' => 'pwd ' ,
441
444
'formFieldRepeat ' => 'pwd_repeat ' ,
442
- 'allowSame ' => false ,
443
445
'current ' => false ,
444
446
'passwordHasher ' => 'Complex ' ,
445
447
]);
@@ -448,42 +450,40 @@ public function testPasswordHasher() {
448
450
'pwd ' => 'somepwd ' ,
449
451
'pwd_repeat ' => 'somepwd '
450
452
];
451
- $ this ->Users ->patchEntity ($ user , $ data );
453
+ $ user ->accessible ('* ' , false ); // Mark all properties as protected
454
+ $ user ->accessible (['id ' ], true );
455
+ $ this ->Users ->patchEntity ($ user , $ data , ['fieldList ' => ['id ' ]]);
452
456
$ result = $ this ->Users ->save ($ user );
453
457
$ this ->assertTrue ((bool )$ result );
454
- $ userCopy = clone ( $ user );
458
+ }
455
459
456
- $ this ->Users ->removeBehavior ('Passwordable ' );
457
- $ this ->Users ->addBehavior ('Tools.Passwordable ' , ['current ' => true ]);
458
- $ user = clone ($ userCopy );
460
+ /**
461
+ * @return void
462
+ */
463
+ public function testPatchWithoutFieldList () {
464
+ $ this ->Users ->addBehavior ('Tools.Passwordable ' , [
465
+ 'formField ' => 'pwd ' ,
466
+ 'formFieldRepeat ' => 'pwd_repeat ' ,
467
+ 'current ' => false ,
468
+ 'passwordHasher ' => 'Complex ' ,
469
+ 'forceFieldList ' => true
470
+ ]);
471
+ $ user = $ this ->Users ->newEntity ();
459
472
$ data = [
460
- 'pwd ' => '123456 ' ,
461
- 'pwd_repeat ' => '12345678 ' ,
473
+ 'name ' => 'x ' ,
474
+ 'pwd ' => 'somepwd ' ,
475
+ 'pwd_repeat ' => 'somepwd '
462
476
];
463
- $ this ->Users ->patchEntity ($ user , $ data );
464
- $ this ->assertTrue ($ this ->Users ->behaviors ()->has ('Passwordable ' ));
465
- $ is = $ this ->Users ->save ($ user );
466
- $ this ->assertFalse ($ is );
477
+ $ user ->accessible ('* ' , false ); // Mark all properties as protected
478
+ $ user ->accessible (['id ' ], true );
479
+ $ user = $ this ->Users ->patchEntity ($ user , $ data );
480
+ $ result = $ this ->Users ->save ($ user );
481
+ $ this ->assertTrue ((bool )$ result );
467
482
468
- $ user = clone ($ userCopy );
469
- $ data = [
470
- 'pwd_current ' => 'somepwdx ' ,
471
- 'pwd ' => '123456 ' ,
472
- 'pwd_repeat ' => '123456 '
473
- ];
474
- $ this ->Users ->patchEntity ($ user , $ data );
475
- $ is = $ this ->Users ->save ($ user );
476
- $ this ->assertFalse ($ is );
483
+ $ savedUser = $ this ->Users ->get ($ user ->id );
477
484
478
- $ user = clone ($ userCopy );
479
- $ data = [
480
- 'pwd_current ' => 'somepwd ' ,
481
- 'pwd ' => '123456 ' ,
482
- 'pwd_repeat ' => '123456 '
483
- ];
484
- $ this ->Users ->patchEntity ($ user , $ data );
485
- $ is = $ this ->Users ->save ($ user );
486
- $ this ->assertTrue (!empty ($ is ));
485
+ $ this ->assertSame ($ data ['name ' ], $ savedUser ->name );
486
+ $ this ->assertSame ($ user ->password , $ savedUser ->password );
487
487
}
488
488
489
489
/**
0 commit comments