1
1
<?php
2
2
3
3
/**
4
+ * @copyright Copyright (c) 2016, ownCloud, Inc.
5
+ *
4
6
* @author Björn Schießle <schiessle@owncloud.com>
7
+ * @author Thomas Citharel <nextcloud@tcit.fr>
5
8
*
6
- * @copyright Copyright (c) 2016, ownCloud, Inc.
7
9
* @license AGPL-3.0
8
10
*
9
11
* This code is free software: you can redistribute it and/or modify
@@ -62,26 +64,25 @@ class AccountManagerTest extends TestCase {
62
64
/** @var IFactory|MockObject */
63
65
protected $ l10nFactory ;
64
66
65
- /** @var \OCP\ IDBConnection */
67
+ /** @var IDBConnection */
66
68
private $ connection ;
67
69
68
- /** @var IConfig|MockObject */
70
+ /** @var IConfig|MockObject */
69
71
private $ config ;
70
72
71
73
/** @var EventDispatcherInterface|MockObject */
72
74
private $ eventDispatcher ;
73
75
74
- /** @var IJobList|MockObject */
76
+ /** @var IJobList|MockObject */
75
77
private $ jobList ;
76
78
77
- /** @var string accounts table name */
78
- private $ table = 'accounts ' ;
79
+ /** accounts table name */
80
+ private string $ table = 'accounts ' ;
79
81
80
82
/** @var LoggerInterface|MockObject */
81
83
private $ logger ;
82
84
83
- /** @var AccountManager */
84
- private $ accountManager ;
85
+ private AccountManager $ accountManager ;
85
86
86
87
protected function setUp (): void {
87
88
parent ::setUp ();
@@ -115,7 +116,7 @@ protected function setUp(): void {
115
116
protected function tearDown (): void {
116
117
parent ::tearDown ();
117
118
$ query = $ this ->connection ->getQueryBuilder ();
118
- $ query ->delete ($ this ->table )->execute ();
119
+ $ query ->delete ($ this ->table )->executeStatement ();
119
120
}
120
121
121
122
protected function makeUser (string $ uid , string $ name , string $ email = null ): IUser {
@@ -423,6 +424,7 @@ protected function populateOrUpdate(): void {
423
424
],
424
425
],
425
426
];
427
+ $ this ->config ->expects ($ this ->exactly (count ($ users )))->method ('getSystemValue ' )->with ('account_manager.default_property_scope ' , [])->willReturn ([]);
426
428
foreach ($ users as $ userInfo ) {
427
429
$ this ->invokePrivate ($ this ->accountManager , 'updateUser ' , [$ userInfo ['user ' ], $ userInfo ['data ' ], null , false ]);
428
430
}
@@ -431,10 +433,9 @@ protected function populateOrUpdate(): void {
431
433
/**
432
434
* get a instance of the accountManager
433
435
*
434
- * @param array $mockedMethods list of methods which should be mocked
435
436
* @return MockObject | AccountManager
436
437
*/
437
- public function getInstance ($ mockedMethods = null ) {
438
+ public function getInstance (? array $ mockedMethods = null ) {
438
439
return $ this ->getMockBuilder (AccountManager::class)
439
440
->setConstructorArgs ([
440
441
$ this ->connection ,
@@ -449,19 +450,15 @@ public function getInstance($mockedMethods = null) {
449
450
$ this ->urlGenerator ,
450
451
$ this ->crypto
451
452
])
452
- ->setMethods ($ mockedMethods )
453
+ ->onlyMethods ($ mockedMethods )
453
454
->getMock ();
454
455
}
455
456
456
457
/**
457
458
* @dataProvider dataTrueFalse
458
459
*
459
- * @param array $newData
460
- * @param array $oldData
461
- * @param bool $insertNew
462
- * @param bool $updateExisting
463
460
*/
464
- public function testUpdateUser ($ newData , $ oldData , $ insertNew , $ updateExisting ) {
461
+ public function testUpdateUser (array $ newData , array $ oldData , bool $ insertNew , bool $ updateExisting ) {
465
462
$ accountManager = $ this ->getInstance (['getUser ' , 'insertNewUser ' , 'updateExistingUser ' ]);
466
463
/** @var IUser $user */
467
464
$ user = $ this ->createMock (IUser::class);
@@ -487,7 +484,6 @@ public function testUpdateUser($newData, $oldData, $insertNew, $updateExisting)
487
484
function ($ eventName , $ event ) use ($ user , $ newData ) {
488
485
$ this ->assertSame ('OC\AccountManager::userUpdated ' , $ eventName );
489
486
$ this ->assertInstanceOf (GenericEvent::class, $ event );
490
- /** @var GenericEvent $event */
491
487
$ this ->assertSame ($ user , $ event ->getSubject ());
492
488
$ this ->assertSame ($ newData , $ event ->getArguments ());
493
489
}
@@ -603,25 +599,14 @@ public function testAddMissingDefaults() {
603
599
'value ' => '1 ' ,
604
600
],
605
601
];
602
+ $ this ->config ->expects ($ this ->once ())->method ('getSystemValue ' )->with ('account_manager.default_property_scope ' , [])->willReturn ([]);
606
603
607
604
$ defaultUserRecord = $ this ->invokePrivate ($ this ->accountManager , 'buildDefaultUserRecord ' , [$ user ]);
608
605
$ result = $ this ->invokePrivate ($ this ->accountManager , 'addMissingDefaultValues ' , [$ input , $ defaultUserRecord ]);
609
606
610
607
$ this ->assertSame ($ expected , $ result );
611
608
}
612
609
613
- private function addDummyValuesToTable ($ uid , $ data ) {
614
- $ query = $ this ->connection ->getQueryBuilder ();
615
- $ query ->insert ($ this ->table )
616
- ->values (
617
- [
618
- 'uid ' => $ query ->createNamedParameter ($ uid ),
619
- 'data ' => $ query ->createNamedParameter (json_encode ($ data )),
620
- ]
621
- )
622
- ->execute ();
623
- }
624
-
625
610
public function testGetAccount () {
626
611
$ accountManager = $ this ->getInstance (['getUser ' ]);
627
612
/** @var IUser $user */
@@ -668,9 +653,6 @@ public function dataParsePhoneNumber(): array {
668
653
669
654
/**
670
655
* @dataProvider dataParsePhoneNumber
671
- * @param string $phoneInput
672
- * @param string $defaultRegion
673
- * @param string|null $phoneNumber
674
656
*/
675
657
public function testParsePhoneNumber (string $ phoneInput , string $ defaultRegion , ?string $ phoneNumber ): void {
676
658
$ this ->config ->method ('getSystemValueString ' )
@@ -790,6 +772,8 @@ public function dataCheckEmailVerification(): array {
790
772
* @dataProvider dataCheckEmailVerification
791
773
*/
792
774
public function testCheckEmailVerification (IUser $ user , ?string $ newEmail ): void {
775
+ // Once because of getAccount, once because of getUser
776
+ $ this ->config ->expects ($ this ->exactly (2 ))->method ('getSystemValue ' )->with ('account_manager.default_property_scope ' , [])->willReturn ([]);
793
777
$ account = $ this ->accountManager ->getAccount ($ user );
794
778
$ emailUpdated = false ;
795
779
@@ -812,4 +796,58 @@ public function testCheckEmailVerification(IUser $user, ?string $newEmail): void
812
796
$ oldData = $ this ->invokePrivate ($ this ->accountManager , 'getUser ' , [$ user , false ]);
813
797
$ this ->invokePrivate ($ this ->accountManager , 'checkEmailVerification ' , [$ account , $ oldData ]);
814
798
}
799
+
800
+ public function dataSetDefaultPropertyScopes (): array {
801
+ return [
802
+ [
803
+ [],
804
+ [
805
+ IAccountManager::PROPERTY_DISPLAYNAME => IAccountManager::SCOPE_FEDERATED ,
806
+ IAccountManager::PROPERTY_ADDRESS => IAccountManager::SCOPE_LOCAL ,
807
+ IAccountManager::PROPERTY_EMAIL => IAccountManager::SCOPE_FEDERATED ,
808
+ IAccountManager::PROPERTY_ROLE => IAccountManager::SCOPE_LOCAL ,
809
+ ]
810
+ ],
811
+ [
812
+ [
813
+ IAccountManager::PROPERTY_DISPLAYNAME => IAccountManager::SCOPE_FEDERATED ,
814
+ IAccountManager::PROPERTY_EMAIL => IAccountManager::SCOPE_LOCAL ,
815
+ IAccountManager::PROPERTY_ROLE => IAccountManager::SCOPE_PRIVATE ,
816
+ ], [
817
+ IAccountManager::PROPERTY_DISPLAYNAME => IAccountManager::SCOPE_FEDERATED ,
818
+ IAccountManager::PROPERTY_EMAIL => IAccountManager::SCOPE_LOCAL ,
819
+ IAccountManager::PROPERTY_ROLE => IAccountManager::SCOPE_PRIVATE ,
820
+ ]
821
+ ],
822
+ [
823
+ [
824
+ IAccountManager::PROPERTY_ADDRESS => 'invalid scope ' ,
825
+ 'invalid property ' => IAccountManager::SCOPE_LOCAL ,
826
+ IAccountManager::PROPERTY_ROLE => IAccountManager::SCOPE_PRIVATE ,
827
+ ],
828
+ [
829
+ IAccountManager::PROPERTY_ADDRESS => IAccountManager::SCOPE_LOCAL ,
830
+ IAccountManager::PROPERTY_EMAIL => IAccountManager::SCOPE_FEDERATED ,
831
+ IAccountManager::PROPERTY_ROLE => IAccountManager::SCOPE_PRIVATE ,
832
+ ]
833
+ ],
834
+ ];
835
+ }
836
+
837
+ /**
838
+ * @dataProvider dataSetDefaultPropertyScopes
839
+ */
840
+ public function testSetDefaultPropertyScopes (array $ propertyScopes , array $ expectedResultScopes ): void {
841
+ $ user = $ this ->makeUser ('steve ' , 'Steve Smith ' , 'steve@steve.steve ' );
842
+ $ this ->config ->expects ($ this ->once ())->method ('getSystemValue ' )->with ('account_manager.default_property_scope ' , [])->willReturn ($ propertyScopes );
843
+
844
+ $ result = $ this ->invokePrivate ($ this ->accountManager , 'buildDefaultUserRecord ' , [$ user ]);
845
+ $ resultProperties = array_column ($ result , 'name ' );
846
+
847
+ $ this ->assertEmpty (array_diff ($ resultProperties , IAccountManager::ALLOWED_PROPERTIES ), "Building default user record returned non-allowed properties " );
848
+ foreach ($ expectedResultScopes as $ expectedResultScopeKey => $ expectedResultScopeValue ) {
849
+ $ resultScope = $ result [array_search ($ expectedResultScopeKey , $ resultProperties )]['scope ' ];
850
+ $ this ->assertEquals ($ expectedResultScopeValue , $ resultScope , "The result scope doesn't follow the value set into the config or defaults correctly. " );
851
+ }
852
+ }
815
853
}
0 commit comments