EntityAccessTest.php 3.95 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
<?php

/**
 * @file
 * Contains Drupal\system\Tests\Entity\EntityAccessTest.
 */

namespace Drupal\system\Tests\Entity;

use Drupal\Core\Language\Language;
use Drupal\Core\TypedData\AccessibleInterface;
use Drupal\user\Plugin\Core\Entity\User;
13
use Drupal\Core\Entity\EntityAccessController;
14 15 16 17

/**
 * Tests the entity access controller.
 */
18 19 20
class EntityAccessTest extends EntityUnitBaseTest  {

  public static $modules = array('language', 'locale');
21 22 23 24 25 26 27 28 29

  public static function getInfo() {
    return array(
      'name' => 'Entity access',
      'description' => 'Tests entity access.',
      'group' => 'Entity API',
    );
  }

30 31 32 33 34 35 36 37 38 39 40 41 42 43
  function setUp() {
    parent::setUp();
    $this->installSchema('user', array('role_permission', 'users_roles'));
    $this->installSchema('system', array('variable', 'url_alias'));
    $this->installSchema('language', 'language');

    // Create the default languages.
    $default_language = language_save(language_default());
    $languages = language_default_locked_languages($default_language->weight);
    foreach ($languages as $language) {
      language_save($language);
    }

  }
44 45 46 47 48 49 50

  /**
   * Asserts entity access correctly grants or denies access.
   */
  function assertEntityAccess($ops, AccessibleInterface $object, User $account = NULL) {
    foreach ($ops as $op => $result) {
      $message = format_string("Entity access returns @result with operation '@op'.", array(
51
        '@result' => !isset($result) ? 'null' : ($result ? 'true' : 'false'),
52 53 54 55 56 57 58 59 60 61 62 63
        '@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.
64 65
    global $user;
    $user = $this->createUser(array('uid' => 2), array('view test entity'));
66 67 68 69
    $entity = entity_create('entity_test', array(
      'name' => 'test',
    ));

70
    // The current user is allowed to view entities.
71
    $this->assertEntityAccess(array(
72 73 74
      'create' => FALSE,
      'update' => FALSE,
      'delete' => FALSE,
75 76 77
      'view' => TRUE,
    ), $entity);

78
    // The custom user is not allowed to perform any operation on test entities.
79
    $custom_user = $this->createUser();
80
    $this->assertEntityAccess(array(
81 82 83
      'create' => FALSE,
      'update' => FALSE,
      'delete' => FALSE,
84 85 86 87 88 89 90 91 92 93
      'view' => FALSE,
    ), $entity, $custom_user);
  }

  /**
   * Ensures that the default controller is used as a fallback.
   */
  function testEntityAccessDefaultController() {
    // Check that the default access controller is used for entities that don't
    // have a specific access controller defined.
94
    $controller = $this->container->get('plugin.manager.entity')->getAccessController('entity_test_default_access');
95
    $this->assertTrue($controller instanceof EntityAccessController, 'The default entity controller is used for the entity_test_default_access entity type.');
96

97
    $entity = entity_create('entity_test_default_access', array());
98 99 100 101 102 103 104 105 106 107 108 109 110 111
    $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.
112 113
    global $user;
    $user = $this->createUser(array('uid' => 2), array('view test entity translations'));
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

    // Create two test languages.
    foreach (array('foo', 'bar') as $langcode) {
      $language = new Language(array(
        'langcode' => $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);
  }
}