Skip to content
Snippets Groups Projects
Select Git revision
  • f9c1ff235b9cb9936cb056d512653554c2ff1a91
  • 11.x default protected
  • 11.2.x protected
  • 10.5.x protected
  • 10.6.x protected
  • 11.1.x protected
  • 10.4.x protected
  • 11.0.x protected
  • 10.3.x protected
  • 7.x protected
  • 10.2.x protected
  • 10.1.x protected
  • 9.5.x protected
  • 10.0.x protected
  • 9.4.x protected
  • 9.3.x protected
  • 9.2.x protected
  • 9.1.x protected
  • 8.9.x protected
  • 9.0.x protected
  • 8.8.x protected
  • 10.5.1 protected
  • 11.2.2 protected
  • 11.2.1 protected
  • 11.2.0 protected
  • 10.5.0 protected
  • 11.2.0-rc2 protected
  • 10.5.0-rc1 protected
  • 11.2.0-rc1 protected
  • 10.4.8 protected
  • 11.1.8 protected
  • 10.5.0-beta1 protected
  • 11.2.0-beta1 protected
  • 11.2.0-alpha1 protected
  • 10.4.7 protected
  • 11.1.7 protected
  • 10.4.6 protected
  • 11.1.6 protected
  • 10.3.14 protected
  • 10.4.5 protected
  • 11.0.13 protected
41 results

EntityAccessControlHandlerTest.php

Blame
  • Alex Pott's avatar
    Issue #2314123 by sun: Fixed various tests.
    Alex Pott authored
    f9c1ff23
    History
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    EntityAccessControlHandlerTest.php 4.26 KiB
    <?php
    
    /**
     * @file
     * Contains \Drupal\system\Tests\Entity\EntityAccessHControlandlerTest.
     */
    
    namespace Drupal\system\Tests\Entity;
    
    use Drupal\Core\Language\Language;
    use Drupal\Core\Session\AccountInterface;
    use Drupal\Core\Access\AccessibleInterface;
    use Drupal\Core\Entity\EntityAccessControlHandler;
    
    /**
     * Tests the entity access control handler.
     *
     * @group Entity
     */
    class EntityAccessControlHandlerTest extends EntityLanguageTestBase  {
    
      function setUp() {
        parent::setUp();
        $this->installSchema('system', 'url_alias');
      }
    
      /**
       * Asserts entity access correctly grants or denies access.
       */
      function assertEntityAccess($ops, AccessibleInterface $object, AccountInterface $account = NULL) {
        foreach ($ops as $op => $result) {
          $message = format_string("Entity access returns @result with operation '@op'.", array(
            '@result' => !isset($result) ? 'null' : ($result ? 'true' : 'false'),
            '@op' => $op,
          ));
    
          $this->assertEqual($result, $object->access($op, $account), $message);
        }
      }
    
      /**
       * Ensures entity access is properly working.
       */
      function testEntityAccess() {
        // Set up a non-admin user that is allowed to view test entities.
        \Drupal::currentUser()->setAccount($this->createUser(array('uid' => 2), array('view test entity')));
        $entity = entity_create('entity_test', array(
          'name' => 'test',
        ));
    
        // The current user is allowed to view entities.
        $this->assertEntityAccess(array(
          'create' => FALSE,
          'update' => FALSE,
          'delete' => FALSE,
          'view' => TRUE,
        ), $entity);
    
        // The custom user is not allowed to perform any operation on test entities.
        $custom_user = $this->createUser();
        $this->assertEntityAccess(array(
          'create' => FALSE,
          'update' => FALSE,
          'delete' => FALSE,
          'view' => FALSE,
        ), $entity, $custom_user);
      }
    
      /**
       * Ensures that the default handler is used as a fallback.
       */
      function testEntityAccessDefaultController() {
        // The implementation requires that the global user id can be loaded.
        \Drupal::currentUser()->setAccount($this->createUser(array('uid' => 2)));
    
        // Check that the default access control handler is used for entities that don't
        // have a specific access control handler defined.
        $handler = $this->container->get('entity.manager')->getAccessControlHandler('entity_test_default_access');
        $this->assertTrue($handler instanceof EntityAccessControlHandler, 'The default entity handler is used for the entity_test_default_access entity type.');
    
        $entity = entity_create('entity_test_default_access');
        $this->assertEntityAccess(array(
          'create' => FALSE,
          'update' => FALSE,
          'delete' => FALSE,
          'view' => FALSE,
        ), $entity);
      }
    
      /**
       * Ensures entity access for entity translations is properly working.
       */
      function testEntityTranslationAccess() {
    
        // Set up a non-admin user that is allowed to view test entity translations.
        \Drupal::currentUser()->setAccount($this->createUser(array('uid' => 2), array('view test entity translations')));
    
        // Create two test languages.
        foreach (array('foo', 'bar') as $langcode) {
          $language = new Language(array(
            'id' => $langcode,
            'name' => $this->randomString(),
          ));
          language_save($language);
        }
    
        $entity = entity_create('entity_test', array(
          'name' => 'test',
          'langcode' => 'foo',
        ));
        $entity->save();
    
        $translation = $entity->getTranslation('bar');
        $this->assertEntityAccess(array(
          'view' => TRUE,
        ), $translation);
      }
    
      /**
       * Tests hook invocations.
       */
      public function testHooks() {
        $state = $this->container->get('state');
        $entity = entity_create('entity_test', array(
          'name' => 'test',
        ));
    
        // Test hook_entity_create_access() and hook_ENTITY_TYPE_create_access().
        $entity->access('create');
        $this->assertEqual($state->get('entity_test_entity_create_access'), TRUE);
        $this->assertEqual($state->get('entity_test_entity_test_create_access'), TRUE);
    
        // Test hook_entity_access() and hook_ENTITY_TYPE_access().
        $entity->access('view');
        $this->assertEqual($state->get('entity_test_entity_access'), TRUE);
        $this->assertEqual($state->get('entity_test_entity_test_access'), TRUE);
      }
    }