/**
  * {@inheritdoc}
  */
 public function setUp()
 {
     parent::setUp();
     $cache_contexts_manager = $this->prophesize(CacheContextsManager::class);
     $cache_contexts_manager->assertValidTokens()->willReturn(TRUE);
     $cache_contexts_manager->reveal();
     $container = new Container();
     $container->set('cache_contexts_manager', $cache_contexts_manager);
     \Drupal::setContainer($container);
     $this->viewer = $this->getMock('\\Drupal\\Core\\Session\\AccountInterface');
     $this->viewer->expects($this->any())->method('hasPermission')->will($this->returnValue(FALSE));
     $this->viewer->expects($this->any())->method('id')->will($this->returnValue(1));
     $this->owner = $this->getMock('\\Drupal\\Core\\Session\\AccountInterface');
     $this->owner->expects($this->any())->method('hasPermission')->will($this->returnValueMap(array(array('administer users', FALSE), array('change own username', TRUE))));
     $this->owner->expects($this->any())->method('id')->will($this->returnValue(2));
     $this->admin = $this->getMock('\\Drupal\\Core\\Session\\AccountInterface');
     $this->admin->expects($this->any())->method('hasPermission')->will($this->returnValue(TRUE));
     $entity_type = $this->getMock('Drupal\\Core\\Entity\\EntityTypeInterface');
     $this->accessControlHandler = new UserAccessControlHandler($entity_type);
     $module_handler = $this->getMock('Drupal\\Core\\Extension\\ModuleHandlerInterface');
     $module_handler->expects($this->any())->method('getImplementations')->will($this->returnValue(array()));
     $this->accessControlHandler->setModuleHandler($module_handler);
     $this->items = $this->getMockBuilder('Drupal\\Core\\Field\\FieldItemList')->disableOriginalConstructor()->getMock();
     $this->items->expects($this->any())->method('defaultAccess')->will($this->returnValue(AccessResult::allowed()));
 }
 /**
  * {@inheritdoc}
  */
 protected function setUp()
 {
     $this->storage = $this->getMock('Drupal\\locale\\StringStorageInterface');
     $this->cache = $this->getMock('Drupal\\Core\\Cache\\CacheBackendInterface');
     $this->lock = $this->getMock('Drupal\\Core\\Lock\\LockBackendInterface');
     $this->lock->expects($this->never())->method($this->anything());
     $this->user = $this->getMock('Drupal\\Core\\Session\\AccountInterface');
     $this->user->expects($this->any())->method('getRoles')->will($this->returnValue(array('anonymous')));
     $this->configFactory = $this->getConfigFactoryStub(array('locale.settings' => array('cache_strings' => FALSE)));
     $this->languageManager = $this->getMock('Drupal\\Core\\Language\\LanguageManagerInterface');
     $container = new ContainerBuilder();
     $container->set('current_user', $this->user);
     \Drupal::setContainer($container);
 }
Esempio n. 3
0
  /**
   * @covers ::setCache
   */
  public function testSetCacheAuthUser() {
    $form_build_id = 'the_form_build_id';
    $form = [];
    $form_state = new FormState();

    $cache_token = 'the_cache_token';
    $form_data = $form;
    $form_data['#cache_token'] = $cache_token;
    $this->formCacheStore->expects($this->once())
      ->method('setWithExpire')
      ->with($form_build_id, $form_data, $this->isType('int'));

    $form_state_data = $form_state->getCacheableArray();
    $form_state_data['build_info']['safe_strings'] = [];
    $this->formStateCacheStore->expects($this->once())
      ->method('setWithExpire')
      ->with($form_build_id, $form_state_data, $this->isType('int'));

    $this->csrfToken->expects($this->once())
      ->method('get')
      ->willReturn($cache_token);
    $this->account->expects($this->once())
      ->method('isAuthenticated')
      ->willReturn(TRUE);

    $this->formCache->setCache($form_build_id, $form, $form_state);
  }
 /**
  * Tests the getUrlIfValid() method where there is no access.
  */
 public function testGetUrlIfValidWithoutAccess()
 {
     $this->account->expects($this->once())->method('hasPermission')->with('link to any page')->willReturn(FALSE);
     $this->accessAwareRouter->expects($this->once())->method('match')->with('/test-path')->willThrowException(new AccessDeniedHttpException());
     $this->pathProcessor->expects($this->once())->method('processInbound')->willReturnArgument(0);
     $url = $this->pathValidator->getUrlIfValid('test-path');
     $this->assertFalse($url);
 }
Esempio n. 5
0
 /**
  * Tests the getUrlIfValidWithoutAccessCheck() method.
  *
  * @covers ::getUrlIfValidWithoutAccessCheck
  */
 public function testGetUrlIfValidWithoutAccessCheck()
 {
     $this->account->expects($this->never())->method('hasPermission')->with('link to any page');
     $this->accessAwareRouter->expects($this->never())->method('match');
     $this->accessUnawareRouter->expects($this->once())->method('match')->with('/test-path')->willReturn([RouteObjectInterface::ROUTE_NAME => 'test_route', '_raw_variables' => new ParameterBag(['key' => 'value'])]);
     $this->pathProcessor->expects($this->once())->method('processInbound')->willReturnArgument(0);
     $url = $this->pathValidator->getUrlIfValidWithoutAccessCheck('test-path');
     $this->assertInstanceOf('Drupal\\Core\\Url', $url);
     $this->assertEquals('test_route', $url->getRouteName());
     $this->assertEquals(['key' => 'value'], $url->getRouteParameters());
 }
Esempio n. 6
0
 /**
  * Test backing out when user is authenticated.
  *
  * @covers ::handle
  * @covers ::__construct
  */
 public function testHandleIsAuthenticated()
 {
     $config_factory = $this->getConfigFactoryStub();
     $cas_subscriber = $this->getMockBuilder('\\Drupal\\cas\\Subscriber\\CasSubscriber')->setConstructorArgs(array($this->requestStack, $this->routeMatcher, $config_factory, $this->currentUser, $this->conditionManager, $this->casHelper))->setMethods(NULL)->getMock();
     $this->event->expects($this->any())->method('getRequestType')->will($this->returnValue(HttpKernelInterface::MASTER_REQUEST));
     $this->currentUser->expects($this->once())->method('isAuthenticated')->will($this->returnValue(TRUE));
     $cas_subscriber->expects($this->never())->method('isIgnoreableRoute');
     $cas_subscriber->expects($this->never())->method('IsCrawlerRequest');
     $cas_subscriber->expects($this->never())->method('handleForcedPath');
     $cas_subscriber->expects($this->never())->method('handleGateway');
     $cas_subscriber->handle($this->event);
 }
 /**
  * @covers ::validateArgument
  *
  * @dataProvider providerValidateArgument
  */
 public function testValidateArgument($expected_validity, $argument, $current_user_id, array $permissions)
 {
     $entity = $this->getMock(EntityInterface::class);
     $entity_storage = $this->getMock(EntityStorageInterface::class);
     $entity_storage->expects($this->atLeastOnce())->method('loadMultiple')->willReturn([7 => $entity, 9 => $entity]);
     $this->entityManager->expects($this->atLeastOnce())->method('getStorage')->with($this->pluginDefinition['entity_type'])->willReturn($entity_storage);
     $this->currentUser->expects($this->any())->method('id')->willReturn($current_user_id);
     $map = [];
     foreach ($permissions as $permission) {
         $map[] = [$permission, TRUE];
     }
     $this->currentUser->expects($this->any())->method('hasPermission')->willReturnMap($map);
     $this->assertSame($expected_validity, $this->sut->validateArgument($argument));
 }
 /**
  * Tests checkAccess() tree manipulator with 'link to any page' permission.
  *
  * @covers ::checkAccess
  * @covers ::menuLinkCheckAccess
  */
 public function testCheckAccessWithLinkToAnyPagePermission()
 {
     $this->mockTree();
     $this->currentUser->expects($this->exactly(8))->method('hasPermission')->with('link to any page')->willReturn(TRUE);
     $this->mockTree();
     $this->defaultMenuTreeManipulators->checkAccess($this->originalTree);
     $this->assertTrue($this->originalTree[1]->access);
     $this->assertTrue($this->originalTree[2]->access);
     $this->assertTrue($this->originalTree[2]->subtree[3]->access);
     $this->assertTrue($this->originalTree[2]->subtree[3]->subtree[4]->access);
     $this->assertTrue($this->originalTree[5]->subtree[7]->access);
     $this->assertTrue($this->originalTree[6]->access);
     $this->assertTrue($this->originalTree[8]->access);
 }
 /**
  * @covers ::execute
  */
 public function testExecute()
 {
     $definitions = ['foo' => ['label' => $this->randomMachineName(), 'description' => $this->randomMachineName()], 'bar' => ['label' => $this->randomMachineName()], 'payment_unavailable' => []];
     $operations_foo = ['baz' => ['title' => $this->randomMachineName()]];
     $operations_provider_foo = $this->getMock(PluginOperationsProviderInterface::class);
     $operations_provider_foo->expects($this->once())->method('getOperations')->with('foo')->willReturn($operations_foo);
     $this->paymentTypeManager->expects($this->once())->method('getDefinitions')->willReturn($definitions);
     $map = [['foo', $operations_provider_foo], ['bar', NULL]];
     $this->paymentTypeManager->expects($this->exactly(2))->method('getOperationsProvider')->willReturnMap($map);
     $this->moduleHandler->expects($this->any())->method('moduleExists')->with('field_ui')->willReturn(TRUE);
     $map = [['administer payment fields', TRUE], ['administer payment form display', TRUE], ['administer payment display', TRUE]];
     $this->currentUser->expects($this->atLeastOnce())->method('hasPermission')->willReturnMap($map);
     $build = $this->sut->execute();
     $expected_build = ['#empty' => 'There are no available payment types.', '#header' => ['Type', 'Description', 'Operations'], '#type' => 'table', 'foo' => ['label' => ['#markup' => $definitions['foo']['label']], 'description' => ['#markup' => $definitions['foo']['description']], 'operations' => ['#links' => $operations_foo + ['configure' => ['url' => new Url('payment.payment_type', ['bundle' => 'foo']), 'title' => 'Configure'], 'manage-fields' => ['title' => 'Manage fields', 'url' => new Url('entity.payment.field_ui_fields', ['bundle' => 'foo'])], 'manage-form-display' => ['title' => 'Manage form display', 'url' => new Url('entity.entity_form_display.payment.default', ['bundle' => 'foo'])], 'manage-display' => ['title' => 'Manage display', 'url' => new Url('entity.entity_view_display.payment.default', ['bundle' => 'foo'])]], '#type' => 'operations']], 'bar' => ['label' => ['#markup' => $definitions['bar']['label']], 'description' => ['#markup' => NULL], 'operations' => ['#links' => ['configure' => ['url' => new Url('payment.payment_type', ['bundle' => 'bar']), 'title' => 'Configure'], 'manage-fields' => ['title' => 'Manage fields', 'url' => new Url('entity.payment.field_ui_fields', ['bundle' => 'bar'])], 'manage-form-display' => ['title' => 'Manage form display', 'url' => new Url('entity.entity_form_display.payment.default', ['bundle' => 'bar'])], 'manage-display' => ['title' => 'Manage display', 'url' => new Url('entity.entity_view_display.payment.default', ['bundle' => 'bar'])]], '#type' => 'operations']]];
     $this->assertEquals($expected_build, $build);
 }
 /**
  * Tests checkAccess() tree manipulator with 'link to any page' permission.
  *
  * @covers ::checkAccess
  * @covers ::menuLinkCheckAccess
  */
 public function testCheckAccessWithLinkToAnyPagePermission()
 {
     $this->mockTree();
     $this->currentUser->expects($this->exactly(9))->method('hasPermission')->with('link to any page')->willReturn(TRUE);
     $this->mockTree();
     $this->defaultMenuTreeManipulators->checkAccess($this->originalTree);
     $expected_access_result = AccessResult::allowed()->cachePerPermissions();
     $this->assertEquals($expected_access_result, $this->originalTree[1]->access);
     $this->assertEquals($expected_access_result, $this->originalTree[2]->access);
     $this->assertEquals($expected_access_result, $this->originalTree[2]->subtree[3]->access);
     $this->assertEquals($expected_access_result, $this->originalTree[2]->subtree[3]->subtree[4]->access);
     $this->assertEquals($expected_access_result, $this->originalTree[5]->subtree[7]->access);
     $this->assertEquals($expected_access_result, $this->originalTree[6]->access);
     $this->assertEquals($expected_access_result, $this->originalTree[8]->access);
     $this->assertEquals($expected_access_result, $this->originalTree[9]->access);
 }
 /**
  * @covers ::formElement
  */
 public function testFormElement()
 {
     $entity_type_id = $this->randomMachineName();
     $bundle = $this->randomMachineName();
     $field_name = $this->randomMachineName();
     $user_id = mt_rand();
     $required = TRUE;
     $entity = $this->getMock(EntityInterface::class);
     $entity->expects($this->atLeastOnce())->method('bundle')->willReturn($bundle);
     $entity->expects($this->atLeastOnce())->method('getEntityTypeId')->willReturn($entity_type_id);
     $this->fieldDefinition->expects($this->once())->method('getName')->willReturn($field_name);
     $this->fieldDefinition->expects($this->once())->method('isRequired')->willReturn($required);
     $payment = $this->getMock(PaymentInterface::class);
     $this->paymentFactory->expects($this->once())->method('createPayment')->with($this->fieldDefinition)->willReturn($payment);
     $this->currentUser->expects($this->exactly(1))->method('id')->willReturn($user_id);
     $items = $this->getMockBuilder(FieldItemList::class)->disableOriginalConstructor()->getMock();
     $items->expects($this->atLeastOnce())->method('getEntity')->willReturn($entity);
     $form = [];
     $form_state = $this->getMock(FormStateInterface::class);
     $build = $this->sut->formElement($items, 3, [], $form, $form_state);
     $expected_build = array('target_id' => array('#default_value' => NULL, '#limit_allowed_plugin_ids' => $this->configFactoryConfiguration['payment_reference.payment_type']['allowed_plugin_ids'], '#plugin_selector_id' => $this->configFactoryConfiguration['payment_reference.payment_type']['plugin_selector_id'], '#prototype_payment' => $payment, '#queue_category_id' => $entity_type_id . '.' . $bundle . '.' . $field_name, '#queue_owner_id' => (int) $user_id, '#required' => $required, '#type' => 'payment_reference'));
     $this->assertSame($expected_build, $build);
 }