Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AdminAccountTest.php
Go to the documentation of this file.
1 <?php
8 
11 
12 class AdminAccountTest extends \PHPUnit\Framework\TestCase
13 {
17  private $dbAdapter;
18 
22  private $encryptor;
23 
27  private $adminAccount;
28 
32  private $prefix;
33 
34  public function setUp()
35  {
36  $this->dbAdapter = $this->getMockBuilder(Mysql::class)
37  ->disableOriginalConstructor()
38  ->getMock();
39 
40  $this->dbAdapter
41  ->method('getTableName')
42  ->willReturnCallback(function ($table) {
43  return $table;
44  });
45 
46  $this->encryptor = $this->getMockBuilder(\Magento\Framework\Encryption\EncryptorInterface::class)
47  ->getMockForAbstractClass();
48 
49  $data = [
53  AdminAccount::KEY_PASSWORD => '123123q',
54  AdminAccount::KEY_USER => 'admin',
55  AdminAccount::KEY_PREFIX => 'pre_',
56  ];
57 
58  $this->prefix = $data[AdminAccount::KEY_PREFIX];
59 
60  $this->adminAccount = new AdminAccount(
61  $this->dbAdapter,
62  $this->encryptor,
63  $data
64  );
65  }
66 
68  {
69  // existing user data
70  $existingUserData = [
71  'email' => '[email protected]',
72  'username' => 'admin',
73  'user_id' => 1,
74  ];
75 
76  // existing admin role data
77  $existingAdminRoleData = [
78  'parent_id' => 0,
79  'tree_level' => 2,
80  'role_type' => 'U',
81  'user_id' => 1,
82  'user_type' => 2,
83  'role_name' => 'admin',
84  'role_id' => 1,
85  ];
86 
87  $returnValueMap = [
88  [
89  'SELECT user_id, username, email FROM ' . $this->prefix .
90  'admin_user WHERE username = :username OR email = :email',
91  ['username' => 'admin', 'email' => '[email protected]'],
92  null,
93  $existingUserData,
94  ],
95  [
96  'SELECT user_id, username, email FROM ' . $this->prefix .
97  'admin_user WHERE username = :username OR email = :email',
98  ['username' => 'admin', 'email' => '[email protected]'],
99  null,
100  $existingUserData,
101  ],
102  [
103  'SELECT * FROM ' . $this->prefix .
104  'authorization_role WHERE user_id = :user_id AND user_type = :user_type',
105  ['user_id' => 1, 'user_type' => 2],
106  null,
107  $existingAdminRoleData,
108  ],
109  ];
110  $this->dbAdapter
111  ->expects($this->exactly(3))
112  ->method('fetchRow')
113  ->will($this->returnValueMap($returnValueMap));
114  $this->dbAdapter->expects($this->once())->method('quoteInto')->will($this->returnValue(''));
115  $this->dbAdapter->expects($this->once())->method('update')->will($this->returnValue(1));
116 
117  $this->dbAdapter->expects($this->once())
118  ->method('insert')
119  ->with($this->equalTo('pre_admin_passwords'), $this->anything());
120 
121  $this->adminAccount->save();
122  }
123 
125  {
126  // existing user data
127  $existingUserData = [
128  'email' => '[email protected]',
129  'username' => 'admin',
130  'user_id' => 1,
131  ];
132 
133  // speical admin role data
134  $administratorRoleData = [
135  'parent_id' => 0,
136  'tree_level' => 1,
137  'role_type' => 'G',
138  'user_id' => 0,
139  'user_type' => 2,
140  'role_name' => 'Administrators',
141  'role_id' => 0,
142  ];
143 
144  $returnValueMap = [
145  [
146  'SELECT user_id, username, email FROM ' . $this->prefix .
147  'admin_user WHERE username = :username OR email = :email',
148  ['username' => 'admin', 'email' => '[email protected]'],
149  null,
150  $existingUserData,
151  ],
152  [
153  'SELECT user_id, username, email FROM ' . $this->prefix .
154  'admin_user WHERE username = :username OR email = :email',
155  ['username' => 'admin', 'email' => '[email protected]'],
156  null,
157  $existingUserData,
158  ],
159  [
160  'SELECT * FROM ' . $this->prefix .
161  'authorization_role WHERE user_id = :user_id AND user_type = :user_type',
162  ['user_id' => 1, 'user_type' => 2],
163  null,
164  [],
165  ],
166  [
167  'SELECT * FROM ' . $this->prefix .
168  'authorization_role WHERE parent_id = :parent_id AND tree_level = :tree_level ' .
169  'AND role_type = :role_type AND user_id = :user_id ' .
170  'AND user_type = :user_type AND role_name = :role_name',
171  [
172  'parent_id' => 0,
173  'tree_level' => 1,
174  'role_type' => 'G',
175  'user_id' => 0,
176  'user_type' => 2,
177  'role_name' => 'Administrators',
178  ],
179  null,
180  $administratorRoleData,
181  ],
182  ];
183 
184  $this->dbAdapter
185  ->expects(self::exactly(4))
186  ->method('fetchRow')
187  ->willReturnMap($returnValueMap);
188  $this->dbAdapter->method('quoteInto')
189  ->willReturn('');
190  $this->dbAdapter->method('update')
191  ->with(self::equalTo('pre_admin_user'), self::anything())
192  ->willReturn(1);
193 
194  $this->dbAdapter->expects(self::at(8))
195  ->method('insert')
196  ->with(self::equalTo('pre_admin_passwords'), self::anything());
197  // should only insert once (admin role)
198  $this->dbAdapter->expects(self::at(14))
199  ->method('insert')
200  ->with(self::equalTo('pre_authorization_role'), self::anything());
201 
202  $this->adminAccount->save();
203  }
204 
206  {
207  // existing admin role data
208  $existingAdminRoleData = [
209  'parent_id' => 0,
210  'tree_level' => 2,
211  'role_type' => 'U',
212  'user_id' => 1,
213  'user_type' => 2,
214  'role_name' => 'admin',
215  'role_id' => 1,
216  ];
217 
218  $returnValueMap = [
219  [
220  'SELECT user_id, username, email FROM ' . $this->prefix .
221  'admin_user WHERE username = :username OR email = :email',
222  ['username' => 'admin', 'email' => '[email protected]'],
223  null,
224  [],
225  ],
226  [
227  'SELECT * FROM ' . $this->prefix .
228  'authorization_role WHERE user_id = :user_id AND user_type = :user_type',
229  ['user_id' => 1, 'user_type' => 2],
230  null,
231  $existingAdminRoleData,
232  ],
233  ];
234 
235  $this->dbAdapter
236  ->expects($this->exactly(2))
237  ->method('fetchRow')
238  ->will($this->returnValueMap($returnValueMap));
239  // insert only once (new user)
240  $this->dbAdapter->expects($this->at(3))
241  ->method('insert')
242  ->with($this->equalTo('pre_admin_user'), $this->anything());
243  $this->dbAdapter->expects($this->at(6))
244  ->method('insert')
245  ->with($this->equalTo('pre_admin_passwords'), $this->anything());
246 
247  // after inserting new user
248  $this->dbAdapter->expects($this->once())->method('lastInsertId')->will($this->returnValue(1));
249 
250  $this->adminAccount->save();
251  }
252 
253  public function testSaveNewUserNewAdminRole()
254  {
255  // special admin role data
256  $administratorRoleData = [
257  'parent_id' => 0,
258  'tree_level' => 1,
259  'role_type' => 'G',
260  'user_id' => 0,
261  'user_type' => 2,
262  'role_name' => 'Administrators',
263  'role_id' => 0,
264  ];
265 
266  $returnValueMap = [
267  [
268  'SELECT user_id, username, email FROM ' . $this->prefix .
269  'admin_user WHERE username = :username OR email = :email',
270  ['username' => 'admin', 'email' => '[email protected]'],
271  null,
272  [],
273  ],
274  [
275  'SELECT * FROM ' . $this->prefix .
276  'authorization_role WHERE user_id = :user_id AND user_type = :user_type',
277  ['user_id' => 1, 'user_type' => 2],
278  null,
279  [],
280  ],
281  [
282  'SELECT * FROM ' . $this->prefix .
283  'authorization_role WHERE parent_id = :parent_id AND tree_level = :tree_level ' .
284  'AND role_type = :role_type AND user_id = :user_id ' .
285  'AND user_type = :user_type AND role_name = :role_name',
286  [
287  'parent_id' => 0,
288  'tree_level' => 1,
289  'role_type' => 'G',
290  'user_id' => 0,
291  'user_type' => 2,
292  'role_name' => 'Administrators',
293  ],
294  null,
295  $administratorRoleData,
296  ]
297 
298  ];
299 
300  $this->dbAdapter
301  ->expects($this->exactly(3))
302  ->method('fetchRow')
303  ->will($this->returnValueMap($returnValueMap));
304  // after inserting new user
305  $this->dbAdapter->expects($this->once())->method('lastInsertId')->will($this->returnValue(1));
306 
307  // insert only (new user and new admin role and new admin password)
308  $this->dbAdapter->expects($this->exactly(3))->method('insert');
309 
310  $this->adminAccount->save();
311  }
312 
318  {
319  // existing user in db
320  $existingUserData = [
321  'email' => '[email protected]',
322  'username' => 'Another.name',
323  ];
324 
325  $this->dbAdapter->expects($this->exactly(2))
326  ->method('fetchRow')->will($this->returnValue($existingUserData));
327  // should not alter db
328  $this->dbAdapter->expects($this->never())->method('update');
329  $this->dbAdapter->expects($this->never())->method('insert');
330 
331  $this->adminAccount->save();
332  }
333 
339  {
340  $existingUserData = [
341  'email' => '[email protected]',
342  'username' => 'admin',
343  ];
344 
345  $this->dbAdapter->expects($this->exactly(2))
346  ->method('fetchRow')->will($this->returnValue($existingUserData));
347  // should not alter db
348  $this->dbAdapter->expects($this->never())->method('update');
349  $this->dbAdapter->expects($this->never())->method('insert');
350 
351  $this->adminAccount->save();
352  }
353 
359  {
360  $this->dbAdapter->expects($this->exactly(3))->method('fetchRow')->will($this->returnValue([]));
361  $this->dbAdapter->expects($this->once())->method('lastInsertId')->will($this->returnValue(1));
362 
363  $this->adminAccount->save();
364  }
365 
371  {
372  // alternative data must be used for this test
373  $data = [
378  AdminAccount::KEY_USER => 'admin',
380  ];
381 
382  $adminAccount = new AdminAccount(
383  $this->dbAdapter,
384  $this->encryptor,
385  $data
386  );
387 
388  // existing user data
389  $existingUserData = [
390  'email' => '[email protected]',
391  'username' => 'passMatch2Username',
392  'user_id' => 1,
393  ];
394 
395  $returnValueMap = [
396  [
397  'SELECT user_id, username, email FROM admin_user WHERE username = :username OR email = :email',
398  ['username' => 'admin', 'email' => '[email protected]'],
399  null,
400  $existingUserData,
401  ]
402 
403  ];
404  $this->dbAdapter
405  ->expects($this->exactly(1))
406  ->method('fetchRow')
407  ->will($this->returnValueMap($returnValueMap));
408  $this->dbAdapter->expects($this->never())->method('insert');
409  $this->dbAdapter->expects($this->never())->method('update');
410 
411  $adminAccount->save();
412  }
413 
419  {
420  // alternative data must be used for this test
421  $data = [
425  AdminAccount::KEY_PASSWORD => 'passMatch2Username',
426  AdminAccount::KEY_USER => 'passMatch2Username',
428  ];
429 
430  $adminAccount = new AdminAccount(
431  $this->dbAdapter,
432  $this->encryptor,
433  $data
434  );
435 
436  // existing user data
437  $existingUserData = [
438  'email' => '[email protected]',
439  'username' => 'passMatch2Username',
440  'user_id' => 1,
441  ];
442 
443  $returnValueMap = [
444  [
445  'SELECT user_id, username, email FROM admin_user WHERE username = :username OR email = :email',
446  ['username' => 'passMatch2Username', 'email' => '[email protected]'],
447  null,
448  $existingUserData,
449  ]
450  ];
451  $this->dbAdapter
452  ->expects($this->exactly(1))
453  ->method('fetchRow')
454  ->will($this->returnValueMap($returnValueMap));
455  $this->dbAdapter->expects($this->never())->method('insert');
456  $this->dbAdapter->expects($this->never())->method('update');
457 
458  $adminAccount->save();
459  }
460 }
$table
Definition: trigger.php:14