]> git.mxchange.org Git - friendica.git/blob - tests/src/Model/UserTest.php
Fixups
[friendica.git] / tests / src / Model / UserTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Test\src\Model;
23
24 use Dice\Dice;
25 use Friendica\Database\Database;
26 use Friendica\DI;
27 use Friendica\Model\User;
28 use Friendica\Test\MockedTest;
29 use Mockery\MockInterface;
30
31 class UserTest extends MockedTest
32 {
33         private $parent;
34         private $child;
35         private $manage;
36
37         /** @var Database|MockInterface */
38         private $dbMock;
39
40         protected function setUp(): void
41         {
42                 parent::setUp();
43
44                 $this->dbMock = \Mockery::mock(Database::class);
45
46                 $diceMock = \Mockery::mock(Dice::class)->makePartial();
47                 /** @var Dice|MockInterface $diceMock */
48                 $diceMock = $diceMock->addRules(include __DIR__ . '/../../../static/dependencies.config.php');
49                 $diceMock->shouldReceive('create')->withArgs([Database::class])->andReturn($this->dbMock);
50                 DI::init($diceMock);
51
52                 $this->parent = [
53                         'uid'      => 1,
54                         'username' => 'maxmuster',
55                         'nickname' => 'Max Muster'
56                 ];
57
58                 $this->child = [
59                         'uid'      => 2,
60                         'username' => 'johndoe',
61                         'nickname' => 'John Doe'
62                 ];
63
64                 $this->manage = [
65                         'uid'      => 3,
66                         'username' => 'janesmith',
67                         'nickname' => 'Jane Smith'
68                 ];
69         }
70
71         public function testIdentitiesEmpty()
72         {
73                 $this->dbMock->shouldReceive('selectFirst')->with('user',
74                         ['uid', 'nickname', 'username', 'parent-uid'],['uid' => $this->parent['uid']], [])->andReturn($this->parent)->once();
75                 $this->dbMock->shouldReceive('isResult')->with($this->parent)->andReturn(false)->once();
76
77                 $record = User::identities($this->parent['uid']);
78
79                 self::assertEquals([], $record);
80         }
81
82         public function testIdentitiesAsParent()
83         {
84                 $parentSelect               = $this->parent;
85                 $parentSelect['parent-uid'] = 0;
86
87                 // Select the user itself (=parent)
88                 $this->dbMock->shouldReceive('selectFirst')->with('user',
89                         ['uid', 'nickname', 'username', 'parent-uid'],['uid' => $this->parent['uid']], [])->andReturn($parentSelect)->once();
90                 $this->dbMock->shouldReceive('isResult')->with($parentSelect)->andReturn(true)->once();
91
92                 // Select one child
93                 $this->dbMock->shouldReceive('select')->with('user',
94                         ['uid', 'username', 'nickname'],
95                         [
96                                 'parent-uid'      => $this->parent['uid'],
97                                 'account_removed' => false
98                         ], [])->andReturn('objectReturn')->once();
99                 $this->dbMock->shouldReceive('isResult')->with('objectReturn')->andReturn(true)->once();
100                 $this->dbMock->shouldReceive('toArray')->with('objectReturn', true, 0)->andReturn([$this->child])->once();
101
102                 // Select the manage
103                 $this->dbMock->shouldReceive('p')->withAnyArgs()->andReturn('objectTwo')->once();
104                 $this->dbMock->shouldReceive('isResult')->with('objectTwo')->andReturn(true)->once();
105                 $this->dbMock->shouldReceive('toArray')->with('objectTwo', true, 0)->andReturn([$this->manage])->once();
106
107                 $record = User::identities($this->parent['uid']);
108
109                 self::assertEquals([
110                         $this->parent,
111                         $this->child,
112                         $this->manage
113                 ], $record);
114         }
115
116         public function testIdentitiesAsChild()
117         {
118                 $childSelect               = $this->child;
119                 $childSelect['parent-uid'] = $this->parent['uid'];
120
121                 // Select the user itself (=child)
122                 $this->dbMock->shouldReceive('selectFirst')->with('user',
123                         ['uid', 'nickname', 'username', 'parent-uid'],['uid' => $this->child['uid']], [])->andReturn($childSelect)->once();
124                 $this->dbMock->shouldReceive('isResult')->with($childSelect)->andReturn(true)->once();
125
126                 // Select the parent
127                 $this->dbMock->shouldReceive('select')->with('user',
128                         ['uid', 'username', 'nickname'],
129                         [
130                                 'uid'      => $this->parent['uid'],
131                                 'account_removed' => false
132                         ], [])->andReturn('objectReturn')->once();
133                 $this->dbMock->shouldReceive('isResult')->with('objectReturn')->andReturn(true)->once();
134                 $this->dbMock->shouldReceive('toArray')->with('objectReturn', true, 0)->andReturn([$this->parent])->once();
135
136                 // Select the childs (user & manage)
137                 $this->dbMock->shouldReceive('select')->with('user',
138                         ['uid', 'username', 'nickname'],
139                         [
140                                 'parent-uid'      => $this->parent['uid'],
141                                 'account_removed' => false
142                         ], [])->andReturn('objectReturn')->once();
143                 $this->dbMock->shouldReceive('isResult')->with('objectReturn')->andReturn(true)->once();
144                 $this->dbMock->shouldReceive('toArray')->with('objectReturn', true, 0)->andReturn([$this->child, $this->manage])->once();
145
146                 // Select the manage
147                 $this->dbMock->shouldReceive('p')->withAnyArgs()->andReturn('objectTwo')->once();
148                 $this->dbMock->shouldReceive('isResult')->with('objectTwo')->andReturn(false)->once();
149
150                 $record = User::identities($this->child['uid']);
151
152                 self::assertEquals([
153                         $this->parent,
154                         $this->child,
155                         $this->manage
156                 ], $record);
157         }
158 }