PageRenderTime 25ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/core/modules/user/tests/src/Unit/Plugin/Validation/Constraint/ProtectedUserFieldConstraintValidatorTest.php

https://gitlab.com/reasonat/test8
PHP | 309 lines | 254 code | 24 blank | 31 comment | 2 complexity | 2e1e882bb1b4ee13a981d799add4c2e1 MD5 | raw file
  1. <?php
  2. namespace Drupal\Tests\user\Unit\Plugin\Validation\Constraint;
  3. use Drupal\Tests\UnitTestCase;
  4. use Drupal\user\Plugin\Validation\Constraint\ProtectedUserFieldConstraint;
  5. use Drupal\user\Plugin\Validation\Constraint\ProtectedUserFieldConstraintValidator;
  6. /**
  7. * @coversDefaultClass \Drupal\user\Plugin\Validation\Constraint\ProtectedUserFieldConstraintValidator
  8. * @group user
  9. */
  10. class ProtectedUserFieldConstraintValidatorTest extends UnitTestCase {
  11. /**
  12. * {@inheritdoc}
  13. */
  14. protected function createValidator() {
  15. // Setup mocks that don't need to change.
  16. $unchanged_field = $this->getMock('Drupal\Core\Field\FieldItemListInterface');
  17. $unchanged_field->expects($this->any())
  18. ->method('getValue')
  19. ->willReturn('unchanged-value');
  20. $unchanged_account = $this->getMock('Drupal\user\UserInterface');
  21. $unchanged_account->expects($this->any())
  22. ->method('get')
  23. ->willReturn($unchanged_field);
  24. $user_storage = $this->getMock('Drupal\user\UserStorageInterface');
  25. $user_storage->expects($this->any())
  26. ->method('loadUnchanged')
  27. ->willReturn($unchanged_account);
  28. $current_user = $this->getMock('Drupal\Core\Session\AccountProxyInterface');
  29. $current_user->expects($this->any())
  30. ->method('id')
  31. ->willReturn('current-user');
  32. return new ProtectedUserFieldConstraintValidator($user_storage, $current_user);
  33. }
  34. /**
  35. * @covers ::validate
  36. *
  37. * @dataProvider providerTestValidate
  38. */
  39. public function testValidate($items, $expected_violation, $name = FALSE) {
  40. $constraint = new ProtectedUserFieldConstraint();
  41. // If a violation is expected, then the context's addViolation method will
  42. // be called, otherwise it should not be called.
  43. $context = $this->getMock('Symfony\Component\Validator\ExecutionContextInterface');
  44. if ($expected_violation) {
  45. $context->expects($this->once())
  46. ->method('addViolation')
  47. ->with($constraint->message, array('%name' => $name));
  48. }
  49. else {
  50. $context->expects($this->never())
  51. ->method('addViolation');
  52. }
  53. $validator = $this->createValidator();
  54. $validator->initialize($context);
  55. $validator->validate($items, $constraint);
  56. }
  57. /**
  58. * Data provider for ::testValidate().
  59. */
  60. public function providerTestValidate() {
  61. $cases = [];
  62. // Case 1: Validation context should not be touched if no items are passed.
  63. $cases[] = [NULL, FALSE];
  64. // Case 2: Empty user should be ignored.
  65. $field_definition = $this->getMock('Drupal\Core\Field\FieldDefinitionInterface');
  66. $items = $this->getMock('Drupal\Core\Field\FieldItemListInterface');
  67. $items->expects($this->once())
  68. ->method('getFieldDefinition')
  69. ->willReturn($field_definition);
  70. $items->expects($this->once())
  71. ->method('getEntity')
  72. ->willReturn(NULL);
  73. $cases[] = [$items, FALSE];
  74. // Case 3: Account flagged to skip protected user should be ignored.
  75. $field_definition = $this->getMock('Drupal\Core\Field\FieldDefinitionInterface');
  76. $account = $this->getMock('Drupal\user\UserInterface');
  77. $account->_skipProtectedUserFieldConstraint = TRUE;
  78. $items = $this->getMock('Drupal\Core\Field\FieldItemListInterface');
  79. $items->expects($this->once())
  80. ->method('getFieldDefinition')
  81. ->willReturn($field_definition);
  82. $items->expects($this->once())
  83. ->method('getEntity')
  84. ->willReturn($account);
  85. $cases[] = [$items, FALSE];
  86. // Case 4: New user should be ignored.
  87. $field_definition = $this->getMock('Drupal\Core\Field\FieldDefinitionInterface');
  88. $account = $this->getMock('Drupal\user\UserInterface');
  89. $account->expects($this->once())
  90. ->method('isNew')
  91. ->willReturn(TRUE);
  92. $items = $this->getMock('Drupal\Core\Field\FieldItemListInterface');
  93. $items->expects($this->once())
  94. ->method('getFieldDefinition')
  95. ->willReturn($field_definition);
  96. $items->expects($this->once())
  97. ->method('getEntity')
  98. ->willReturn($account);
  99. $cases[] = [$items, FALSE];
  100. // Case 5: Mismatching user IDs should also be ignored.
  101. $account = $this->getMock('Drupal\user\UserInterface');
  102. $account->expects($this->once())
  103. ->method('isNew')
  104. ->willReturn(FALSE);
  105. $account->expects($this->once())
  106. ->method('id')
  107. ->willReturn('not-current-user');
  108. $items = $this->getMock('Drupal\Core\Field\FieldItemListInterface');
  109. $items->expects($this->once())
  110. ->method('getFieldDefinition')
  111. ->willReturn($field_definition);
  112. $items->expects($this->once())
  113. ->method('getEntity')
  114. ->willReturn($account);
  115. $cases[] = [$items, FALSE];
  116. // Case 6: Non-password fields that have not changed should be ignored.
  117. $field_definition = $this->getMock('Drupal\Core\Field\FieldDefinitionInterface');
  118. $field_definition->expects($this->exactly(2))
  119. ->method('getName')
  120. ->willReturn('field_not_password');
  121. $account = $this->getMock('Drupal\user\UserInterface');
  122. $account->expects($this->once())
  123. ->method('isNew')
  124. ->willReturn(FALSE);
  125. $account->expects($this->exactly(2))
  126. ->method('id')
  127. ->willReturn('current-user');
  128. $account->expects($this->never())
  129. ->method('checkExistingPassword');
  130. $items = $this->getMock('Drupal\Core\Field\FieldItemListInterface');
  131. $items->expects($this->once())
  132. ->method('getFieldDefinition')
  133. ->willReturn($field_definition);
  134. $items->expects($this->once())
  135. ->method('getEntity')
  136. ->willReturn($account);
  137. $items->expects($this->once())
  138. ->method('getValue')
  139. ->willReturn('unchanged-value');
  140. $cases[] = [$items, FALSE];
  141. // Case 7: Password field with no value set should be ignored.
  142. $field_definition = $this->getMock('Drupal\Core\Field\FieldDefinitionInterface');
  143. $field_definition->expects($this->once())
  144. ->method('getName')
  145. ->willReturn('pass');
  146. $account = $this->getMock('Drupal\user\UserInterface');
  147. $account->expects($this->once())
  148. ->method('isNew')
  149. ->willReturn(FALSE);
  150. $account->expects($this->exactly(2))
  151. ->method('id')
  152. ->willReturn('current-user');
  153. $account->expects($this->never())
  154. ->method('checkExistingPassword');
  155. $items = $this->getMock('Drupal\Core\Field\FieldItemListInterface');
  156. $items->expects($this->once())
  157. ->method('getFieldDefinition')
  158. ->willReturn($field_definition);
  159. $items->expects($this->once())
  160. ->method('getEntity')
  161. ->willReturn($account);
  162. $cases[] = [$items, FALSE];
  163. // Case 8: Non-password field changed, but user has passed provided current
  164. // password.
  165. $field_definition = $this->getMock('Drupal\Core\Field\FieldDefinitionInterface');
  166. $field_definition->expects($this->exactly(2))
  167. ->method('getName')
  168. ->willReturn('field_not_password');
  169. $account = $this->getMock('Drupal\user\UserInterface');
  170. $account->expects($this->once())
  171. ->method('isNew')
  172. ->willReturn(FALSE);
  173. $account->expects($this->exactly(2))
  174. ->method('id')
  175. ->willReturn('current-user');
  176. $account->expects($this->once())
  177. ->method('checkExistingPassword')
  178. ->willReturn(TRUE);
  179. $items = $this->getMock('Drupal\Core\Field\FieldItemListInterface');
  180. $items->expects($this->once())
  181. ->method('getFieldDefinition')
  182. ->willReturn($field_definition);
  183. $items->expects($this->once())
  184. ->method('getEntity')
  185. ->willReturn($account);
  186. $items->expects($this->once())
  187. ->method('getValue')
  188. ->willReturn('changed-value');
  189. $cases[] = [$items, FALSE];
  190. // Case 9: Password field changed, current password confirmed.
  191. $field_definition = $this->getMock('Drupal\Core\Field\FieldDefinitionInterface');
  192. $field_definition->expects($this->exactly(2))
  193. ->method('getName')
  194. ->willReturn('pass');
  195. $account = $this->getMock('Drupal\user\UserInterface');
  196. $account->expects($this->once())
  197. ->method('isNew')
  198. ->willReturn(FALSE);
  199. $account->expects($this->exactly(2))
  200. ->method('id')
  201. ->willReturn('current-user');
  202. $account->expects($this->once())
  203. ->method('checkExistingPassword')
  204. ->willReturn(TRUE);
  205. $items = $this->getMock('Drupal\Core\Field\FieldItemListInterface');
  206. $items->expects($this->once())
  207. ->method('getFieldDefinition')
  208. ->willReturn($field_definition);
  209. $items->expects($this->once())
  210. ->method('getEntity')
  211. ->willReturn($account);
  212. $items->expects($this->any())
  213. ->method('getValue')
  214. ->willReturn('changed-value');
  215. $items->expects($this->once())
  216. ->method('__get')
  217. ->with('value')
  218. ->willReturn('changed-value');
  219. $cases[] = [$items, FALSE];
  220. // The below calls should result in a violation.
  221. // Case 10: Password field changed, current password not confirmed.
  222. $field_definition = $this->getMock('Drupal\Core\Field\FieldDefinitionInterface');
  223. $field_definition->expects($this->exactly(2))
  224. ->method('getName')
  225. ->willReturn('pass');
  226. $field_definition->expects($this->any())
  227. ->method('getLabel')
  228. ->willReturn('Password');
  229. $account = $this->getMock('Drupal\user\UserInterface');
  230. $account->expects($this->once())
  231. ->method('isNew')
  232. ->willReturn(FALSE);
  233. $account->expects($this->exactly(2))
  234. ->method('id')
  235. ->willReturn('current-user');
  236. $account->expects($this->once())
  237. ->method('checkExistingPassword')
  238. ->willReturn(FALSE);
  239. $items = $this->getMock('Drupal\Core\Field\FieldItemListInterface');
  240. $items->expects($this->once())
  241. ->method('getFieldDefinition')
  242. ->willReturn($field_definition);
  243. $items->expects($this->once())
  244. ->method('getEntity')
  245. ->willReturn($account);
  246. $items->expects($this->once())
  247. ->method('getValue')
  248. ->willReturn('changed-value');
  249. $items->expects($this->once())
  250. ->method('__get')
  251. ->with('value')
  252. ->willReturn('changed-value');
  253. $cases[] = [$items, TRUE, 'Password'];
  254. // Case 11: Non-password field changed, current password not confirmed.
  255. $field_definition = $this->getMock('Drupal\Core\Field\FieldDefinitionInterface');
  256. $field_definition->expects($this->exactly(2))
  257. ->method('getName')
  258. ->willReturn('field_not_password');
  259. $field_definition->expects($this->any())
  260. ->method('getLabel')
  261. ->willReturn('Protected field');
  262. $account = $this->getMock('Drupal\user\UserInterface');
  263. $account->expects($this->once())
  264. ->method('isNew')
  265. ->willReturn(FALSE);
  266. $account->expects($this->exactly(2))
  267. ->method('id')
  268. ->willReturn('current-user');
  269. $account->expects($this->once())
  270. ->method('checkExistingPassword')
  271. ->willReturn(FALSE);
  272. $items = $this->getMock('Drupal\Core\Field\FieldItemListInterface');
  273. $items->expects($this->once())
  274. ->method('getFieldDefinition')
  275. ->willReturn($field_definition);
  276. $items->expects($this->once())
  277. ->method('getEntity')
  278. ->willReturn($account);
  279. $items->expects($this->once())
  280. ->method('getValue')
  281. ->willReturn('changed-value');
  282. $cases[] = [$items, TRUE, 'Protected field'];
  283. return $cases;
  284. }
  285. }