PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/src/Unit/Entity/PaymentMethodConfiguration/PaymentMethodConfigurationAccessControlHandlerTest.php

https://gitlab.com/Drulenium-bot/payment
PHP | 357 lines | 228 code | 47 blank | 82 comment | 0 complexity | 7382d65fd38de80e5e702f6a6728a553 MD5 | raw file
  1. <?php
  2. namespace Drupal\Tests\payment\Unit\Entity\PaymentMethodConfiguration;
  3. use Drupal\Core\Cache\Context\CacheContextsManager;
  4. use Drupal\Core\DependencyInjection\Container;
  5. use Drupal\Core\Entity\EntityTypeInterface;
  6. use Drupal\Core\Extension\ModuleHandlerInterface;
  7. use Drupal\Core\Language\LanguageInterface;
  8. use Drupal\Core\Session\AccountInterface;
  9. use Drupal\payment\Entity\PaymentMethodConfiguration\PaymentMethodConfigurationAccessControlHandler;
  10. use Drupal\payment\Entity\PaymentMethodConfigurationInterface;
  11. use Drupal\Tests\UnitTestCase;
  12. use Symfony\Component\DependencyInjection\ContainerInterface;
  13. /**
  14. * @coversDefaultClass \Drupal\payment\Entity\PaymentMethodConfiguration\PaymentMethodConfigurationAccessControlHandler
  15. *
  16. * @group Payment
  17. */
  18. class PaymentMethodConfigurationAccessControlHandlerTest extends UnitTestCase {
  19. /**
  20. * The module handler.
  21. *
  22. * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $moduleHandler;
  25. /**
  26. * The class under test.
  27. *
  28. * @var \Drupal\payment\Entity\PaymentMethodConfiguration\PaymentMethodConfigurationAccessControlHandler
  29. */
  30. protected $sut;
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function setUp() {
  35. $cache_context_manager = $this->getMockBuilder(CacheContextsManager::class)
  36. ->disableOriginalConstructor()
  37. ->getMock();
  38. $cache_context_manager->expects($this->any())
  39. ->method('assertValidTokens')
  40. ->willReturn(TRUE);
  41. $container = new Container();
  42. $container->set('cache_contexts_manager', $cache_context_manager);
  43. \Drupal::setContainer($container);
  44. $entity_type = $this->getMock(EntityTypeInterface::class);
  45. $this->moduleHandler = $this->getMock(ModuleHandlerInterface::class);
  46. $this->moduleHandler->expects($this->any())
  47. ->method('invokeAll')
  48. ->willReturn([]);
  49. $this->sut = new PaymentMethodConfigurationAccessControlHandler($entity_type, $this->moduleHandler);
  50. }
  51. /**
  52. * @covers ::createInstance
  53. * @covers ::__construct
  54. */
  55. public function testCreateInstance() {
  56. $container = $this->getMock(ContainerInterface::class);
  57. $map = [
  58. ['module_handler', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $this->moduleHandler],
  59. ];
  60. $container->expects($this->any())
  61. ->method('get')
  62. ->willReturnMap($map);
  63. $entity_type = $this->getMock(EntityTypeInterface::class);
  64. $handler = PaymentMethodConfigurationAccessControlHandler::createInstance($container, $entity_type);
  65. $this->assertInstanceOf(PaymentMethodConfigurationAccessControlHandler::class, $handler);
  66. }
  67. /**
  68. * Gets a mock payment method configuration.
  69. *
  70. * @return \Drupal\payment\Entity\PaymentMethodConfiguration|\PHPUnit_Framework_MockObject_MockObject
  71. */
  72. protected function getMockPaymentMethodConfiguration() {
  73. $payment_method_configuration = $this->getMock(PaymentMethodConfigurationInterface::class);
  74. $payment_method_configuration->expects($this->any())
  75. ->method('getCacheContexts')
  76. ->willReturn([]);
  77. $payment_method_configuration->expects($this->any())
  78. ->method('getCacheTags')
  79. ->willReturn(['payment_method_configuration']);
  80. return $payment_method_configuration;
  81. }
  82. /**
  83. * @covers ::checkAccess
  84. */
  85. public function testCheckAccessWithoutPermission() {
  86. $operation = $this->randomMachineName();
  87. $account = $this->getMock(AccountInterface::class);
  88. $account->expects($this->any())
  89. ->method('hasPermission')
  90. ->willReturn(FALSE);
  91. $payment_method_configuration = $this->getMockPaymentMethodConfiguration();
  92. $class = new \ReflectionClass($this->sut);
  93. $method = $class->getMethod('checkAccess');
  94. $method->setAccessible(TRUE);
  95. $this->assertFalse($method->invokeArgs($this->sut, [$payment_method_configuration, $operation, $account])->isAllowed());
  96. }
  97. /**
  98. * @covers ::checkAccess
  99. */
  100. public function testCheckAccessWithAnyPermission() {
  101. $operation = $this->randomMachineName();
  102. $account = $this->getMock(AccountInterface::class);
  103. $map = [
  104. ['payment.payment_method_configuration.' . $operation . '.any', TRUE],
  105. ['payment.payment_method_configuration.' . $operation . '.own', FALSE],
  106. ];
  107. $account->expects($this->any())
  108. ->method('hasPermission')
  109. ->willReturnMap($map);
  110. $payment_method_configuration = $this->getMockPaymentMethodConfiguration();
  111. $class = new \ReflectionClass($this->sut);
  112. $method = $class->getMethod('checkAccess');
  113. $method->setAccessible(TRUE);
  114. $this->assertTrue($method->invokeArgs($this->sut, [$payment_method_configuration, $operation, $account])->isAllowed());
  115. }
  116. /**
  117. * @covers ::checkAccess
  118. */
  119. public function testCheckAccessWithOwnPermission() {
  120. $owner_id = mt_rand();
  121. $operation = $this->randomMachineName();
  122. $account = $this->getMock(AccountInterface::class);
  123. $account->expects($this->any())
  124. ->method('id')
  125. ->willReturn($owner_id);
  126. $map = [
  127. ['payment.payment_method_configuration.' . $operation . '.any', FALSE],
  128. ['payment.payment_method_configuration.' . $operation . '.own', TRUE],
  129. ];
  130. $account->expects($this->any())
  131. ->method('hasPermission')
  132. ->willReturnMap($map);
  133. $payment_method_configuration = $this->getMockPaymentMethodConfiguration();
  134. $payment_method_configuration->expects($this->at(0))
  135. ->method('getOwnerId')
  136. ->willReturn($owner_id);
  137. $payment_method_configuration->expects($this->at(1))
  138. ->method('getOwnerId')
  139. ->willReturn($owner_id + 1);
  140. $class = new \ReflectionClass($this->sut);
  141. $method = $class->getMethod('checkAccess');
  142. $method->setAccessible(TRUE);
  143. $this->assertTrue($method->invokeArgs($this->sut, [$payment_method_configuration, $operation, $account])->isAllowed());
  144. $this->assertFalse($method->invokeArgs($this->sut, [$payment_method_configuration, $operation, $account])->isAllowed());
  145. }
  146. /**
  147. * @covers ::checkAccess
  148. *
  149. * @dataProvider providerTestCheckAccessEnable
  150. */
  151. public function testCheckAccessEnable($expected, $payment_method_configuration_status, $has_update_permission) {
  152. $operation = 'enable';
  153. $account = $this->getMock(AccountInterface::class);
  154. $map = [
  155. ['payment.payment_method_configuration.update.any', $has_update_permission],
  156. ['payment.payment_method_configuration.update.own', FALSE],
  157. ];
  158. $account->expects($this->atLeastOnce())
  159. ->method('hasPermission')
  160. ->willReturnMap($map);
  161. $payment_method_configuration = $this->getMockPaymentMethodConfiguration();
  162. $payment_method_configuration->expects($this->atLeastOnce())
  163. ->method('status')
  164. ->willReturn($payment_method_configuration_status);
  165. $this->setUpLanguage($payment_method_configuration);
  166. $class = new \ReflectionClass($this->sut);
  167. $method = $class->getMethod('checkAccess');
  168. $method->setAccessible(TRUE);
  169. $this->assertSame($expected, $method->invokeArgs($this->sut, [$payment_method_configuration, $operation, $account])->isAllowed());
  170. }
  171. /**
  172. * Provides data to self::testCheckAccessEnable().
  173. */
  174. public function providerTestCheckAccessEnable() {
  175. return [
  176. // Enabled with permission.
  177. [FALSE, TRUE, TRUE],
  178. // Disabled with permission.
  179. [TRUE, FALSE, TRUE],
  180. // Disabled without permission.
  181. [FALSE, FALSE, FALSE],
  182. ];
  183. }
  184. /**
  185. * @covers ::checkAccess
  186. *
  187. * @dataProvider providerTestCheckAccessDisable
  188. */
  189. public function testCheckAccessDisable($expected, $payment_method_configuration_status, $has_update_permission) {
  190. $operation = 'disable';
  191. $account = $this->getMock(AccountInterface::class);
  192. $map = [
  193. ['payment.payment_method_configuration.update.any', $has_update_permission],
  194. ['payment.payment_method_configuration.update.own', FALSE],
  195. ];
  196. $account->expects($this->atLeastOnce())
  197. ->method('hasPermission')
  198. ->willReturnMap($map);
  199. $payment_method_configuration = $this->getMockPaymentMethodConfiguration();
  200. $payment_method_configuration->expects($this->atLeastOnce())
  201. ->method('status')
  202. ->willReturn($payment_method_configuration_status);
  203. $this->setUpLanguage($payment_method_configuration);
  204. $class = new \ReflectionClass($this->sut);
  205. $method = $class->getMethod('checkAccess');
  206. $method->setAccessible(TRUE);
  207. $this->assertSame($expected, $method->invokeArgs($this->sut, [$payment_method_configuration, $operation, $account])->isAllowed());
  208. }
  209. /**
  210. * Provides data to self::testCheckAccessDisable().
  211. */
  212. public function providerTestCheckAccessDisable() {
  213. return [
  214. // Disabled with permission.
  215. [FALSE, FALSE, TRUE],
  216. // Enabled with permission.
  217. [TRUE, TRUE, TRUE],
  218. // Enabled without permission.
  219. [FALSE, TRUE, FALSE],
  220. ];
  221. }
  222. /**
  223. * @covers ::checkAccess
  224. *
  225. * @dataProvider providerTestCheckAccessDuplicate
  226. */
  227. public function testCheckAccessDuplicate($expected, $has_create_permission, $has_view_permission) {
  228. $operation = 'duplicate';
  229. $bundle = $this->randomMachineName();
  230. $account = $this->getMock(AccountInterface::class);
  231. $map = [
  232. ['payment.payment_method_configuration.create.' . $bundle, $has_create_permission],
  233. ['payment.payment_method_configuration.view.any', $has_view_permission],
  234. ];
  235. $account->expects($this->any())
  236. ->method('hasPermission')
  237. ->willReturnMap($map);
  238. $language = $this->getMock(LanguageInterface::class);
  239. $payment_method_configuration = $this->getMockPaymentMethodConfiguration();
  240. $payment_method_configuration->expects($this->atLeastOnce())
  241. ->method('bundle')
  242. ->willReturn($bundle);
  243. $payment_method_configuration->expects($this->any())
  244. ->method('language')
  245. ->willReturn($language);
  246. $class = new \ReflectionClass($this->sut);
  247. $method = $class->getMethod('checkAccess');
  248. $method->setAccessible(TRUE);
  249. $this->assertSame($expected, $method->invokeArgs($this->sut, [$payment_method_configuration, $operation, $account])->isAllowed());
  250. }
  251. /**
  252. * Provides data to self::testCheckAccessDuplicate().
  253. */
  254. public function providerTestCheckAccessDuplicate() {
  255. return [
  256. // No create access.
  257. [FALSE, FALSE, TRUE],
  258. // Create access, with view permission.
  259. [TRUE, TRUE, TRUE],
  260. // Create access, without view permission.
  261. [FALSE, TRUE, FALSE],
  262. // No access.
  263. [FALSE, FALSE, FALSE],
  264. ];
  265. }
  266. /**
  267. * @covers ::checkCreateAccess
  268. */
  269. public function testCheckCreateAccess() {
  270. $bundle = $this->randomMachineName();
  271. $context = [];
  272. $account = $this->getMock(AccountInterface::class);
  273. $account->expects($this->once())
  274. ->method('hasPermission')
  275. ->with('payment.payment_method_configuration.create.' . $bundle)
  276. ->willReturn(TRUE);
  277. $class = new \ReflectionClass($this->sut);
  278. $method = $class->getMethod('checkCreateAccess');
  279. $method->setAccessible(TRUE);
  280. $this->assertTrue($method->invokeArgs($this->sut, [$account, $context, $bundle])->isAllowed());
  281. }
  282. /**
  283. * @covers ::getCache
  284. */
  285. public function testGetCache() {
  286. $account = $this->getMock(AccountInterface::class);
  287. $cache_id = $this->randomMachineName();
  288. $operation = $this->randomMachineName();
  289. $language_code = $this->randomMachineName();
  290. $class = new \ReflectionClass($this->sut);
  291. $method = $class->getMethod('getCache');
  292. $method->setAccessible(TRUE);
  293. $this->assertNull($method->invokeArgs($this->sut, [$cache_id, $operation, $language_code, $account]));
  294. }
  295. /**
  296. * Sets up the mock definitions for the language() method.
  297. *
  298. * @param \PHPUnit_Framework_MockObject_MockObject $payment_method_configuration
  299. * A mock entity.
  300. */
  301. protected function setUpLanguage(\PHPUnit_Framework_MockObject_MockObject $payment_method_configuration) {
  302. $language = $this->getMock(LanguageInterface::class);
  303. $language->expects($this->any())
  304. ->method('getId')
  305. ->willReturn($this->randomMachineName(2));
  306. $payment_method_configuration->expects($this->any())
  307. ->method('language')
  308. ->willReturn($language);
  309. }
  310. }