editAccessCheck = new EditEntityFieldAccessCheck(); } /** * Provides test data for testAccess(). * * @see \Drupal\edit\Tests\quickedit\Access\EditEntityFieldAccessCheckTest::testAccess() */ public function providerTestAccess() { $editable_entity = $this->createMockEntity(); $editable_entity->expects($this->any()) ->method('access') ->will($this->returnValue(TRUE)); $non_editable_entity = $this->createMockEntity(); $non_editable_entity->expects($this->any()) ->method('access') ->will($this->returnValue(FALSE)); $field_storage_with_access = $this->getMockBuilder('Drupal\field\Entity\FieldStorageConfig') ->disableOriginalConstructor() ->getMock(); $field_storage_with_access->expects($this->any()) ->method('access') ->will($this->returnValue(TRUE)); $field_storage_without_access = $this->getMockBuilder('Drupal\field\Entity\FieldStorageConfig') ->disableOriginalConstructor() ->getMock(); $field_storage_without_access->expects($this->any()) ->method('access') ->will($this->returnValue(FALSE)); $data = array(); $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); return $data; } /** * Tests the method for checking access to routes. * * @param \Drupal\Core\Entity\EntityInterface $entity * A mocked entity. * @param \Drupal\field\FieldStorageConfigInterface $field_storage * A mocked field storage. * @param bool|null $expected_result * The expected result of the access call. * * @dataProvider providerTestAccess */ public function testAccess(EntityInterface $entity, FieldStorageConfigInterface $field_storage = NULL, $expected_result) { $field_name = 'valid'; $entity_with_field = clone $entity; $entity_with_field->expects($this->any()) ->method('get') ->with($field_name) ->will($this->returnValue($field_storage)); $entity_with_field->expects($this->once()) ->method('hasTranslation') ->with(LanguageInterface::LANGCODE_NOT_SPECIFIED) ->will($this->returnValue(TRUE)); $account = $this->getMock('Drupal\Core\Session\AccountInterface'); $access = $this->editAccessCheck->access($entity_with_field, $field_name, LanguageInterface::LANGCODE_NOT_SPECIFIED, $account); $this->assertSame($expected_result, $access); } /** * Tests checking access to routes that result in AccessCheckInterface::KILL. * * @dataProvider providerTestAccessKill */ public function testAccessKill($field_name, $langcode) { $account = $this->getMock('Drupal\Core\Session\AccountInterface'); $entity = $this->createMockEntity(); $this->assertSame(AccessCheckInterface::KILL, $this->editAccessCheck->access($entity, $field_name, $langcode, $account)); } /** * Provides test data for testAccessKill. */ 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; } /** * Returns a mock entity. * * @return \Drupal\Core\Entity\EntityInterface|\PHPUnit_Framework_MockObject_MockObject */ protected function createMockEntity() { $entity = $this->getMockBuilder('Drupal\entity_test\Entity\EntityTest') ->disableOriginalConstructor() ->getMock(); $entity->expects($this->any()) ->method('hasTranslation') ->will($this->returnValueMap(array( array(LanguageInterface::LANGCODE_NOT_SPECIFIED, TRUE), array('xx-lolspeak', FALSE), ))); $entity->expects($this->any()) ->method('hasField') ->will($this->returnValueMap(array( array('valid', TRUE), array('not_valid', FALSE), ))); return $entity; } }