installConfig(['field']); // The users table is needed for creating dummy user accounts. $this->installEntitySchema('user'); // Register entity_test text field. module_load_install('entity_test'); entity_test_install(); } /** * Tests hook_entity_field_access() and hook_entity_field_access_alter(). * * @see entity_test_entity_field_access() * @see entity_test_entity_field_access_alter() */ public function testFieldAccess() { $values = [ 'name' => $this->randomMachineName(), 'user_id' => 1, 'field_test_text' => [ 'value' => 'no access value', 'format' => 'full_html', ], ]; $entity = EntityTest::create($values); // Create a dummy user account for testing access with. $values = ['name' => 'test']; $account = User::create($values); $this->assertFalse($entity->field_test_text->access('view', $account), 'Access to the field was denied.'); $expected = AccessResult::forbidden()->addCacheableDependency($entity); $this->assertEqual($expected, $entity->field_test_text->access('view', $account, TRUE), 'Access to the field was denied.'); $entity->field_test_text = 'access alter value'; $this->assertFalse($entity->field_test_text->access('view', $account), 'Access to the field was denied.'); $this->assertEqual($expected, $entity->field_test_text->access('view', $account, TRUE), 'Access to the field was denied.'); $entity->field_test_text = 'standard value'; $this->assertTrue($entity->field_test_text->access('view', $account), 'Access to the field was granted.'); $this->assertEqual(AccessResult::allowed(), $entity->field_test_text->access('view', $account, TRUE), 'Access to the field was granted.'); } }