/** * 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'))); // Use the 'entity_test_label' entity type in order to test the 'view label' // access operation. $entity = EntityTestLabel::create(array('name' => 'test')); // The current user is allowed to view entities. $this->assertEntityAccess(array('create' => FALSE, 'update' => FALSE, 'delete' => FALSE, 'view' => TRUE, 'view label' => TRUE), $entity); // The custom user is not allowed to perform any operation on test entities, // except for viewing their label. $custom_user = $this->createUser(); $this->assertEntityAccess(array('create' => FALSE, 'update' => FALSE, 'delete' => FALSE, 'view' => FALSE, 'view label' => TRUE), $entity, $custom_user); }
/** * Tests the label formatter. */ public function testLabelFormatter() { /** @var \Drupal\Core\Render\RendererInterface $renderer */ $renderer = $this->container->get('renderer'); $formatter = 'entity_reference_label'; // The 'link' settings is TRUE by default. $build = $this->buildRenderArray([$this->referencedEntity, $this->unsavedReferencedEntity], $formatter); $expected_field_cacheability = ['contexts' => [], 'tags' => [], 'max-age' => Cache::PERMANENT]; $this->assertEqual($build['#cache'], $expected_field_cacheability, 'The field render array contains the entity access cacheability metadata'); $expected_item_1 = array('#type' => 'link', '#title' => $this->referencedEntity->label(), '#url' => $this->referencedEntity->urlInfo(), '#options' => $this->referencedEntity->urlInfo()->getOptions(), '#cache' => array('contexts' => ['user.permissions'], 'tags' => $this->referencedEntity->getCacheTags())); $this->assertEqual($renderer->renderRoot($build[0]), $renderer->renderRoot($expected_item_1), sprintf('The markup returned by the %s formatter is correct for an item with a saved entity.', $formatter)); $this->assertEqual(CacheableMetadata::createFromRenderArray($build[0]), CacheableMetadata::createFromRenderArray($expected_item_1)); // The second referenced entity is "autocreated", therefore not saved and // lacking any URL info. $expected_item_2 = array('#plain_text' => $this->unsavedReferencedEntity->label(), '#cache' => array('contexts' => ['user.permissions'], 'tags' => $this->unsavedReferencedEntity->getCacheTags(), 'max-age' => Cache::PERMANENT)); $this->assertEqual($build[1], $expected_item_2, sprintf('The render array returned by the %s formatter is correct for an item with a unsaved entity.', $formatter)); // Test with the 'link' setting set to FALSE. $build = $this->buildRenderArray([$this->referencedEntity, $this->unsavedReferencedEntity], $formatter, array('link' => FALSE)); $this->assertEqual($build[0]['#plain_text'], $this->referencedEntity->label(), sprintf('The markup returned by the %s formatter is correct for an item with a saved entity.', $formatter)); $this->assertEqual($build[1]['#plain_text'], $this->unsavedReferencedEntity->label(), sprintf('The markup returned by the %s formatter is correct for an item with a unsaved entity.', $formatter)); // Test an entity type that doesn't have any link templates, which means // \Drupal\Core\Entity\EntityInterface::urlInfo() will throw an exception // and the label formatter will output only the label instead of a link. $field_storage_config = FieldStorageConfig::loadByName($this->entityType, $this->fieldName); $field_storage_config->setSetting('target_type', 'entity_test_label'); $field_storage_config->save(); $referenced_entity_with_no_link_template = EntityTestLabel::create(array('name' => $this->randomMachineName())); $referenced_entity_with_no_link_template->save(); $build = $this->buildRenderArray([$referenced_entity_with_no_link_template], $formatter, array('link' => TRUE)); $this->assertEqual($build[0]['#plain_text'], $referenced_entity_with_no_link_template->label(), sprintf('The markup returned by the %s formatter is correct for an entity type with no valid link template.', $formatter)); }
/** * Tests CRUD operations. */ function testCRUD() { $default_langcode = \Drupal::languageManager()->getDefaultLanguage()->getId(); // Verify default properties on a newly created empty entity. $empty = EntityTestLabel::create(); $this->assertIdentical($empty->id->value, NULL); $this->assertIdentical($empty->name->value, NULL); $this->assertTrue($empty->uuid->value); $this->assertIdentical($empty->langcode->value, $default_langcode); // Verify ConfigEntity properties/methods on the newly created empty entity. $this->assertIdentical($empty->isNew(), TRUE); $this->assertIdentical($empty->bundle(), 'entity_test_label'); $this->assertIdentical($empty->id(), NULL); $this->assertTrue($empty->uuid()); $this->assertIdentical($empty->label(), NULL); // Verify Entity properties/methods on the newly created empty entity. $this->assertIdentical($empty->getEntityTypeId(), 'entity_test_label'); // The URI can only be checked after saving. try { $empty->urlInfo(); $this->fail('EntityMalformedException was thrown.'); } catch (EntityMalformedException $e) { $this->pass('EntityMalformedException was thrown.'); } // Verify that an empty entity cannot be saved. try { $empty->save(); $this->fail('EntityMalformedException was thrown.'); } catch (EntityMalformedException $e) { $this->pass('EntityMalformedException was thrown.'); } // Verify that an entity with an empty ID string is considered empty, too. $empty_id = EntityTestLabel::create(array('id' => '')); $this->assertIdentical($empty_id->isNew(), TRUE); try { $empty_id->save(); $this->fail('EntityMalformedException was thrown.'); } catch (EntityMalformedException $e) { $this->pass('EntityMalformedException was thrown.'); } // Verify properties on a newly created entity. $entity_test = EntityTestLabel::create($expected = array('id' => $this->randomMachineName(), 'name' => $this->randomString())); $this->assertIdentical($entity_test->id->value, $expected['id']); $this->assertTrue($entity_test->uuid->value); $this->assertNotEqual($entity_test->uuid->value, $empty->uuid->value); $this->assertIdentical($entity_test->name->value, $expected['name']); $this->assertIdentical($entity_test->langcode->value, $default_langcode); // Verify methods on the newly created entity. $this->assertIdentical($entity_test->isNew(), TRUE); $this->assertIdentical($entity_test->id(), $expected['id']); $this->assertTrue($entity_test->uuid()); $expected['uuid'] = $entity_test->uuid(); $this->assertIdentical($entity_test->label(), $expected['name']); // Verify that the entity can be saved. try { $status = $entity_test->save(); $this->pass('EntityMalformedException was not thrown.'); } catch (EntityMalformedException $e) { $this->fail('EntityMalformedException was not thrown.'); } // Verify that the correct status is returned and properties did not change. $this->assertIdentical($status, SAVED_NEW); $this->assertIdentical($entity_test->id(), $expected['id']); $this->assertIdentical($entity_test->uuid(), $expected['uuid']); $this->assertIdentical($entity_test->label(), $expected['name']); $this->assertIdentical($entity_test->isNew(), FALSE); // Save again, and verify correct status and properties again. $status = $entity_test->save(); $this->assertIdentical($status, SAVED_UPDATED); $this->assertIdentical($entity_test->id(), $expected['id']); $this->assertIdentical($entity_test->uuid(), $expected['uuid']); $this->assertIdentical($entity_test->label(), $expected['name']); $this->assertIdentical($entity_test->isNew(), FALSE); // Ensure that creating an entity with the same id as an existing one is not // possible. $same_id = EntityTestLabel::create(array('id' => $entity_test->id())); $this->assertIdentical($same_id->isNew(), TRUE); try { $same_id->save(); $this->fail('Not possible to overwrite an entity entity.'); } catch (EntityStorageException $e) { $this->pass('Not possible to overwrite an entity entity.'); } // Verify that renaming the ID returns correct status and properties. $ids = array($expected['id'], 'second_' . $this->randomMachineName(4), 'third_' . $this->randomMachineName(4)); for ($i = 1; $i < 3; $i++) { $old_id = $ids[$i - 1]; $new_id = $ids[$i]; // Before renaming, everything should point to the current ID. $this->assertIdentical($entity_test->id(), $old_id); // Rename. $entity_test->id = $new_id; $this->assertIdentical($entity_test->id(), $new_id); $status = $entity_test->save(); $this->assertIdentical($status, SAVED_UPDATED); $this->assertIdentical($entity_test->isNew(), FALSE); // Verify that originalID points to new ID directly after renaming. $this->assertIdentical($entity_test->id(), $new_id); } }