EditEntityFieldAccessCheckTest.php 5.15 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\quickedit\Tests\Access\EditEntityFieldAccessCheckTest.
6 7
 */

8
namespace Drupal\quickedit\Tests\Access;
9 10

use Drupal\Core\Access\AccessCheckInterface;
11
use Drupal\quickedit\Access\EditEntityFieldAccessCheck;
12
use Drupal\Tests\UnitTestCase;
13
use Drupal\field\FieldStorageConfigInterface;
14
use Drupal\Core\Entity\EntityInterface;
15
use Drupal\Core\Language\LanguageInterface;
16 17

/**
18 19
 * @coversDefaultClass \Drupal\quickedit\Access\EditEntityFieldAccessCheck
 * @group quickedit
20 21 22 23 24 25
 */
class EditEntityFieldAccessCheckTest extends UnitTestCase {

  /**
   * The tested access checker.
   *
26
   * @var \Drupal\quickedit\Access\EditEntityFieldAccessCheck
27 28 29 30
   */
  protected $editAccessCheck;

  /**
31
   * {@inheritdoc}
32 33
   */
  protected function setUp() {
34
    $this->editAccessCheck = new EditEntityFieldAccessCheck();
35 36 37 38 39
  }

  /**
   * Provides test data for testAccess().
   *
40
   * @see \Drupal\edit\Tests\quickedit\Access\EditEntityFieldAccessCheckTest::testAccess()
41 42
   */
  public function providerTestAccess() {
43
    $editable_entity = $this->createMockEntity();
44 45 46 47
    $editable_entity->expects($this->any())
      ->method('access')
      ->will($this->returnValue(TRUE));

48
    $non_editable_entity = $this->createMockEntity();
49 50 51 52
    $non_editable_entity->expects($this->any())
      ->method('access')
      ->will($this->returnValue(FALSE));

53
    $field_storage_with_access = $this->getMockBuilder('Drupal\field\Entity\FieldStorageConfig')
54 55
      ->disableOriginalConstructor()
      ->getMock();
56
    $field_storage_with_access->expects($this->any())
57 58
      ->method('access')
      ->will($this->returnValue(TRUE));
59
    $field_storage_without_access = $this->getMockBuilder('Drupal\field\Entity\FieldStorageConfig')
60 61
      ->disableOriginalConstructor()
      ->getMock();
62
    $field_storage_without_access->expects($this->any())
63 64 65 66
      ->method('access')
      ->will($this->returnValue(FALSE));

    $data = array();
67 68 69 70
    $data[] = array($editable_entity, $field_storage_with_access, AccessCheckInterface::ALLOW);
    $data[] = array($non_editable_entity, $field_storage_with_access, AccessCheckInterface::DENY);
    $data[] = array($editable_entity, $field_storage_without_access, AccessCheckInterface::DENY);
    $data[] = array($non_editable_entity, $field_storage_without_access, AccessCheckInterface::DENY);
71 72 73 74 75 76 77 78 79

    return $data;
  }

  /**
   * Tests the method for checking access to routes.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   A mocked entity.
80 81
   * @param \Drupal\field\FieldStorageConfigInterface $field_storage
   *   A mocked field storage.
82 83 84 85 86
   * @param bool|null $expected_result
   *   The expected result of the access call.
   *
   * @dataProvider providerTestAccess
   */
87
  public function testAccess(EntityInterface $entity, FieldStorageConfigInterface $field_storage = NULL, $expected_result) {
88
    $field_name = 'valid';
89 90 91
    $entity_with_field = clone $entity;
    $entity_with_field->expects($this->any())
      ->method('get')
92
      ->with($field_name)
93
      ->will($this->returnValue($field_storage));
94 95
    $entity_with_field->expects($this->once())
      ->method('hasTranslation')
96
      ->with(LanguageInterface::LANGCODE_NOT_SPECIFIED)
97
      ->will($this->returnValue(TRUE));
98

99
    $account = $this->getMock('Drupal\Core\Session\AccountInterface');
100
    $access = $this->editAccessCheck->access($entity_with_field, $field_name, LanguageInterface::LANGCODE_NOT_SPECIFIED, $account);
101 102 103 104
    $this->assertSame($expected_result, $access);
  }

  /**
105 106 107
   * Tests checking access to routes that result in AccessCheckInterface::KILL.
   *
   * @dataProvider providerTestAccessKill
108
   */
109
  public function testAccessKill($field_name, $langcode) {
110
    $account = $this->getMock('Drupal\Core\Session\AccountInterface');
111 112
    $entity = $this->createMockEntity();
    $this->assertSame(AccessCheckInterface::KILL, $this->editAccessCheck->access($entity, $field_name, $langcode, $account));
113 114 115
  }

  /**
116
   * Provides test data for testAccessKill.
117
   */
118 119 120 121 122 123 124 125 126 127 128
  public function providerTestAccessKill() {
    $data = array();
    // Tests the access method without a field_name.
    $data[] = array(NULL, LanguageInterface::LANGCODE_NOT_SPECIFIED);
    // Tests the access method with a non-existent field.
    $data[] = array('not_valid', LanguageInterface::LANGCODE_NOT_SPECIFIED);
    // Tests the access method without a langcode.
    $data[] = array('valid', NULL);
    // Tests the access method with an invalid langcode.
    $data[] = array('valid', 'xx-lolspeak');
    return $data;
129 130 131 132
  }

  /**
   * Returns a mock entity.
133 134
   *
   * @return \Drupal\Core\Entity\EntityInterface|\PHPUnit_Framework_MockObject_MockObject
135 136 137
   */
  protected function createMockEntity() {
    $entity = $this->getMockBuilder('Drupal\entity_test\Entity\EntityTest')
138 139 140
      ->disableOriginalConstructor()
      ->getMock();

141 142 143 144 145 146
    $entity->expects($this->any())
      ->method('hasTranslation')
      ->will($this->returnValueMap(array(
        array(LanguageInterface::LANGCODE_NOT_SPECIFIED, TRUE),
        array('xx-lolspeak', FALSE),
      )));
147 148 149 150 151 152
    $entity->expects($this->any())
      ->method('hasField')
      ->will($this->returnValueMap(array(
        array('valid', TRUE),
        array('not_valid', FALSE),
      )));
153

154
    return $entity;
155 156 157
  }

}