Issue #1893108 by Berdir, dawehner: Convert most entity tests to DrupalUnitTestBase.

8.0.x
webchick 2013-02-27 21:18:48 -05:00
parent aab2a8cde4
commit d4c21cd945
21 changed files with 390 additions and 284 deletions

View File

@ -1,64 +0,0 @@
<?php
/**
* @file
* Definition of Drupal\field\Tests\EntityPropertiesTest.
*/
namespace Drupal\field\Tests;
/**
* Tests entity properties.
*/
class EntityPropertiesTest extends FieldTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('field_test');
public static function getInfo() {
return array(
'name' => 'Entity properties',
'description' => 'Tests entity properties.',
'group' => 'Entity API',
);
}
/**
* Tests label key and label callback of an entity.
*/
function testEntityLabel() {
$entity_types = array(
'test_entity_no_label',
'test_entity_label',
'test_entity_label_callback',
);
// @todo Remove once test_entity entity has been merged with entity_test.
$values = array(
'ftlabel' => $this->randomName(),
);
foreach ($entity_types as $entity_type) {
$entity = entity_create($entity_type, $values);
$label = $entity->label();
switch ($entity_type) {
case 'test_entity_no_label':
$this->assertFalse($label, 'Entity with no label property or callback returned FALSE.');
break;
case 'test_entity_label':
$this->assertEqual($label, $entity->ftlabel, 'Entity with label key returned correct label.');
break;
case 'test_entity_label_callback':
$this->assertEqual($label, 'label callback ' . $entity->ftlabel, 'Entity with label callback returned correct label.');
break;
}
}
}
}

View File

@ -186,23 +186,6 @@ function field_test_field_create_field($field) {
field_test_memorize(__FUNCTION__, $args);
}
/**
* Entity label callback.
*
* @param $entity_type
* The entity type.
* @param $entity
* The entity object.
* @param $langcocde
* (optional) The langcode.
*
* @return
* The label of the entity prefixed with "label callback".
*/
function field_test_entity_label_callback($entity_type, $entity, $langcode = NULL) {
return 'label callback ' . $entity->ftlabel;
}
/**
* Implements hook_field_attach_view_alter().
*/

View File

@ -1,35 +0,0 @@
<?php
/**
* @file
* Contains Drupal\field_test\Plugin\Core\Entity\LabelCallbackTestEntity.
*/
namespace Drupal\field_test\Plugin\Core\Entity;
use Drupal\Core\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;
/**
* Test entity class.
*
* @Plugin(
* id = "test_entity_label_callback",
* label = @Translation("Test entity label callback"),
* module = "field_test",
* controller_class = "Drupal\field_test\TestEntityController",
* field_cache = FALSE,
* base_table = "test_entity",
* revision_table = "test_entity_revision",
* label_callback = "field_test_entity_label_callback",
* fieldable = TRUE,
* entity_keys = {
* "id" = "ftid",
* "revision" = "ftvid",
* "bundle" = "fttype"
* }
* )
*/
class LabelCallbackTestEntity extends TestEntity {
}

View File

@ -1,38 +0,0 @@
<?php
/**
* @file
* Contains Drupal\field_test\Plugin\Core\Entity\LabelTestEntity.
*/
namespace Drupal\field_test\Plugin\Core\Entity;
use Drupal\Core\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;
/**
* Test entity class.
*
* @Plugin(
* id = "test_entity_label",
* label = @Translation("Test Entity label"),
* module = "field_test",
* controller_class = "Drupal\field_test\TestEntityController",
* form_controller_class = {
* "default" = "Drupal\field_test\TestEntityFormController"
* },
* field_cache = FALSE,
* base_table = "test_entity",
* revision_table = "test_entity_revision",
* fieldable = TRUE,
* entity_keys = {
* "id" = "ftid",
* "revision" = "ftvid",
* "bundle" = "fttype",
* "label" = "ftlabel"
* }
* )
*/
class LabelTestEntity extends TestEntity {
}

View File

@ -1,37 +0,0 @@
<?php
/**
* @file
* Contains Drupal\field_test\Plugin\Core\Entity\NoLabelTestEntity.
*/
namespace Drupal\field_test\Plugin\Core\Entity;
use Drupal\Core\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;
/**
* Test entity class.
*
* @Plugin(
* id = "test_entity_no_label",
* label = @Translation("Test Entity without label"),
* module = "field_test",
* controller_class = "Drupal\field_test\TestEntityController",
* form_controller_class = {
* "default" = "Drupal\field_test\TestEntityFormController"
* },
* field_cache = FALSE,
* base_table = "test_entity",
* revision_table = "test_entity_revision",
* fieldable = TRUE,
* entity_keys = {
* "id" = "ftid",
* "revision" = "ftvid",
* "bundle" = "fttype"
* }
* )
*/
class NoLabelTestEntity extends TestEntity {
}

View File

@ -9,14 +9,15 @@ namespace Drupal\system\Tests\Entity;
use Drupal\Core\Language\Language;
use Drupal\Core\TypedData\AccessibleInterface;
use Drupal\simpletest\WebTestBase;
use Drupal\user\Plugin\Core\Entity\User;
use Drupal\Core\Entity\EntityAccessController;
/**
* Tests the entity access controller.
*/
class EntityAccessTest extends WebTestBase {
class EntityAccessTest extends EntityUnitBaseTest {
public static $modules = array('language', 'locale');
public static function getInfo() {
return array(
@ -26,12 +27,20 @@ class EntityAccessTest extends WebTestBase {
);
}
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('entity_test');
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);
}
}
/**
* Asserts entity access correctly grants or denies access.
@ -39,7 +48,7 @@ class EntityAccessTest extends WebTestBase {
function assertEntityAccess($ops, AccessibleInterface $object, User $account = NULL) {
foreach ($ops as $op => $result) {
$message = format_string("Entity access returns @result with operation '@op'.", array(
'@result' => isset($result) ? 'null' : ($result ? 'true' : 'false'),
'@result' => !isset($result) ? 'null' : ($result ? 'true' : 'false'),
'@op' => $op,
));
@ -52,24 +61,22 @@ class EntityAccessTest extends WebTestBase {
*/
function testEntityAccess() {
// Set up a non-admin user that is allowed to view test entities.
$user = $this->drupalCreateUser(array('view test entity'));
$this->drupalLogin($user);
global $user;
$user = $this->createUser(array('uid' => 2), array('view test entity'));
$entity = entity_create('entity_test', array(
'name' => 'test',
));
$entity->save();
// The current user is allowed to view, create, update and delete entities.
// The current user is allowed to view entities.
$this->assertEntityAccess(array(
'create' => TRUE,
'update' => TRUE,
'delete' => TRUE,
'create' => FALSE,
'update' => FALSE,
'delete' => FALSE,
'view' => TRUE,
), $entity);
// The custom user is not allowed to perform any operation on test entities.
$custom_user = $this->drupalCreateUser();
$custom_user = $this->createUser();
$this->assertEntityAccess(array(
'create' => FALSE,
'update' => FALSE,
@ -100,13 +107,10 @@ class EntityAccessTest extends WebTestBase {
* Ensures entity access for entity translations is properly working.
*/
function testEntityTranslationAccess() {
// Enable translations for the test entity type.
variable_set('entity_test_translation', TRUE);
module_enable(array('locale'));
// Set up a non-admin user that is allowed to view test entity translations.
$user = $this->drupalCreateUser(array('view test entity translations'));
$this->drupalLogin($user);
global $user;
$user = $this->createUser(array('uid' => 2), array('view test entity translations'));
// Create two test languages.
foreach (array('foo', 'bar') as $langcode) {

View File

@ -7,19 +7,12 @@
namespace Drupal\system\Tests\Entity;
use Drupal\simpletest\WebTestBase;
use Drupal\user\Plugin\Core\Entity\User;
/**
* Tests the basic Entity API.
*/
class EntityApiTest extends WebTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('entity_test');
class EntityApiTest extends EntityUnitBaseTest {
public static function getInfo() {
return array(
@ -29,15 +22,26 @@ class EntityApiTest extends WebTestBase {
);
}
public function setUp() {
parent::setUp();
$this->installSchema('entity_test', array(
'entity_test_mul',
'entity_test_mul_property_data',
'entity_test_rev',
'entity_test_rev_revision',
'entity_test_mulrev',
'entity_test_mulrev_property_data',
'entity_test_mulrev_property_revision'
));
}
/**
* Tests basic CRUD functionality of the Entity API.
*/
public function testCRUD() {
$user1 = $this->drupalCreateUser();
// All entity variations have to have the same results.
foreach (entity_test_entity_types() as $entity_type) {
$this->assertCRUD($entity_type, $user1);
$this->assertCRUD($entity_type, $this->createUser());
}
}
@ -49,7 +53,7 @@ class EntityApiTest extends WebTestBase {
* @param \Drupal\user\Plugin\Core\Entity\User $user1
* The user to run the tests with.
*/
protected function assertCRUD($entity_type, \Drupal\user\Plugin\Core\Entity\User $user1) {
protected function assertCRUD($entity_type, User $user1) {
// Create some test entities.
$entity = entity_create($entity_type, array('name' => 'test', 'user_id' => $user1->uid));
$entity->save();

View File

@ -7,7 +7,6 @@
namespace Drupal\system\Tests\Entity;
use Drupal\simpletest\WebTestBase;
use Drupal\Core\Database\Database;
/**
@ -22,14 +21,14 @@ use Drupal\Core\Database\Database;
* As well as all type-specific hooks, like hook_node_insert(),
* hook_comment_update(), etc.
*/
class EntityCrudHookTest extends WebTestBase {
class EntityCrudHookTest extends EntityUnitBaseTest {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('entity_crud_hook_test', 'taxonomy', 'block_test', 'block', 'comment', 'file', 'entity_test');
public static $modules = array('block', 'block_test', 'entity_crud_hook_test', 'file', 'taxonomy', 'node', 'comment');
protected $ids = array();
@ -41,6 +40,13 @@ class EntityCrudHookTest extends WebTestBase {
);
}
public function setUp() {
parent::setUp();
$this->installSchema('user', array('users_roles', 'users_data'));
$this->installSchema('node', array('node', 'node_revision', 'node_type', 'node_access'));
$this->installSchema('comment', array('comment', 'node_comment_statistics'));
}
/**
* Checks the order of CRUD hook execution messages.
*
@ -124,8 +130,10 @@ class EntityCrudHookTest extends WebTestBase {
* Tests hook invocations for CRUD operations on comments.
*/
public function testCommentHooks() {
$account = $this->createUser();
$node = entity_create('node', array(
'uid' => 1,
'uid' => $account->uid,
'type' => 'article',
'title' => 'Test node',
'status' => 1,
@ -145,7 +153,7 @@ class EntityCrudHookTest extends WebTestBase {
'cid' => NULL,
'pid' => 0,
'nid' => $nid,
'uid' => 1,
'uid' => $account->uid,
'subject' => 'Test comment',
'created' => REQUEST_TIME,
'changed' => REQUEST_TIME,
@ -202,6 +210,7 @@ class EntityCrudHookTest extends WebTestBase {
* Tests hook invocations for CRUD operations on files.
*/
public function testFileHooks() {
$this->installSchema('file', array('file_managed', 'file_usage'));
$url = 'public://entity_crud_hook_test.file';
file_put_contents($url, 'Test test test');
$file = entity_create('file', array(
@ -264,8 +273,10 @@ class EntityCrudHookTest extends WebTestBase {
* Tests hook invocations for CRUD operations on nodes.
*/
public function testNodeHooks() {
$account = $this->createUser();
$node = entity_create('node', array(
'uid' => 1,
'uid' => $account->id(),
'type' => 'article',
'title' => 'Test node',
'status' => 1,
@ -326,6 +337,8 @@ class EntityCrudHookTest extends WebTestBase {
* Tests hook invocations for CRUD operations on taxonomy terms.
*/
public function testTaxonomyTermHooks() {
$this->installSchema('taxonomy', array('taxonomy_term_data', 'taxonomy_term_hierarchy'));
$vocabulary = entity_create('taxonomy_vocabulary', array(
'name' => 'Test vocabulary',
'vid' => 'test',
@ -393,6 +406,8 @@ class EntityCrudHookTest extends WebTestBase {
* Tests hook invocations for CRUD operations on taxonomy vocabularies.
*/
public function testTaxonomyVocabularyHooks() {
$this->installSchema('taxonomy', array('taxonomy_term_data', 'taxonomy_term_hierarchy'));
$vocabulary = entity_create('taxonomy_vocabulary', array(
'name' => 'Test vocabulary',
'vid' => 'test',

View File

@ -11,19 +11,18 @@ use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Field\FieldInterface;
use Drupal\Core\Entity\Field\FieldItemInterface;
use Drupal\Core\TypedData\TypedDataInterface;
use Drupal\simpletest\WebTestBase;
/**
* Tests Entity API base functionality.
*/
class EntityFieldTest extends WebTestBase {
class EntityFieldTest extends EntityUnitBaseTest {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('entity_test');
public static $modules = array('filter', 'text', 'node');
public static function getInfo() {
return array(
@ -33,6 +32,27 @@ class EntityFieldTest extends WebTestBase {
);
}
public function setUp() {
parent::setUp();
$this->installSchema('user', array('users_roles', 'users_data'));
$this->installSchema('node', array('node', 'node_revision', 'node_type', 'node_access'));
$this->installSchema('entity_test', array(
'entity_test_mul',
'entity_test_mul_property_data',
'entity_test_rev',
'entity_test_rev_revision',
'entity_test_mulrev',
'entity_test_mulrev_property_data',
'entity_test_mulrev_property_revision'
));
// Create the test field.
entity_test_install();
// Install required default configuration for filter module.
$this->installConfig(array('system', 'filter'));
}
/**
* Creates a test entity.
*
@ -40,7 +60,7 @@ class EntityFieldTest extends WebTestBase {
*/
protected function createTestEntity($entity_type) {
$this->entity_name = $this->randomName();
$this->entity_user = $this->drupalCreateUser();
$this->entity_user = $this->createUser();
$this->entity_field_text = $this->randomName();
// Pass in the value of the name field when creating. With the user
@ -100,13 +120,13 @@ class EntityFieldTest extends WebTestBase {
$this->assertEqual($this->entity_user->name, $entity->user_id->entity->name, format_string('%entity_type: User name can be read.', array('%entity_type' => $entity_type)));
// Change the assigned user by entity.
$new_user = $this->drupalCreateUser();
$new_user = $this->createUser();
$entity->user_id->entity = $new_user;
$this->assertEqual($new_user->uid, $entity->user_id->target_id, format_string('%entity_type: Updated user id can be read.', array('%entity_type' => $entity_type)));
$this->assertEqual($new_user->name, $entity->user_id->entity->name, format_string('%entity_type: Updated user name value can be read.', array('%entity_type' => $entity_type)));
// Change the assigned user by id.
$new_user = $this->drupalCreateUser();
$new_user = $this->createUser();
$entity->user_id->target_id = $new_user->uid;
$this->assertEqual($new_user->uid, $entity->user_id->target_id, format_string('%entity_type: Updated user id can be read.', array('%entity_type' => $entity_type)));
$this->assertEqual($new_user->name, $entity->user_id->entity->name, format_string('%entity_type: Updated user name value can be read.', array('%entity_type' => $entity_type)));
@ -185,7 +205,7 @@ class EntityFieldTest extends WebTestBase {
// Test creating the entity by passing in plain values.
$this->entity_name = $this->randomName();
$name_item[0]['value'] = $this->entity_name;
$this->entity_user = $this->drupalCreateUser();
$this->entity_user = $this->createUser();
$user_item[0]['target_id'] = $this->entity_user->uid;
$this->entity_field_text = $this->randomName();
$text_item[0]['value'] = $this->entity_field_text;
@ -523,7 +543,14 @@ class EntityFieldTest extends WebTestBase {
$this->assertEqual($violations->count(), 0);
// Test validating an entity of the wrong type.
$node = $this->drupalCreateNode(array('type' => 'page'));
$user = $this->createUser();
$user->save();
$node = entity_create('node', array(
'type' => 'page',
'uid' => $user->id(),
));
// @todo: EntityWrapper can only handle entities with an id.
$node->save();
$wrapped_entity->setValue($node);
$violations = $wrapped_entity->validate();
$this->assertEqual($violations->count(), 1);

View File

@ -0,0 +1,55 @@
<?php
/**
* @file
* Contains \Drupal\system\Tests\Entity\EntityLabelTest.
*/
namespace Drupal\system\Tests\Entity;
/**
* Tests entity properties.
*/
class EntityLabelTest extends EntityUnitBaseTest {
public static function getInfo() {
return array(
'name' => 'Entity label',
'description' => 'Tests entity labels.',
'group' => 'Entity API',
);
}
/**
* Tests label key and label callback of an entity.
*/
function testEntityLabel() {
$entity_types = array(
'entity_test_no_label',
'entity_test_label',
'entity_test_label_callback',
);
$values = array(
'name' => $this->randomName(),
);
foreach ($entity_types as $entity_type) {
$entity = entity_create($entity_type, $values);
$label = $entity->label();
switch ($entity_type) {
case 'entity_test_no_label':
$this->assertFalse($label, 'Entity with no label property or callback returned FALSE.');
break;
case 'entity_test_label':
$this->assertEqual($label, $entity->name->value, 'Entity with label key returned correct label.');
break;
case 'entity_test_label_callback':
$this->assertEqual($label, 'label callback ' . $entity->name->value, 'Entity with label callback returned correct label.');
break;
}
}
}
}

View File

@ -7,19 +7,17 @@
namespace Drupal\system\Tests\Entity;
use Drupal\simpletest\WebTestBase;
/**
* Tests Entity Query API relationship functionality.
*/
class EntityQueryRelationshipTest extends WebTestBase {
class EntityQueryRelationshipTest extends EntityUnitBaseTest {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('entity_test', 'taxonomy');
public static $modules = array('taxonomy', 'options');
/**
* @var \Drupal\field_sql_storage\Entity\QueryFactory
@ -69,8 +67,11 @@ class EntityQueryRelationshipTest extends WebTestBase {
);
}
protected function setUp() {
public function setUp() {
parent::setUp();
$this->installSchema('taxonomy', array('taxonomy_term_data', 'taxonomy_term_hierarchy'));
// We want a taxonomy term reference field. It needs a vocabulary, terms,
// a field and an instance. First, create the vocabulary.
$vocabulary = entity_create('taxonomy_vocabulary', array(
@ -100,7 +101,7 @@ class EntityQueryRelationshipTest extends WebTestBase {
));
$term->save();
$this->terms[] = $term;
$this->accounts[] = $this->drupalCreateUser();
$this->accounts[] = $this->createUser();
}
// Create three entity_test entities, the 0th entity will point to the
// 0th account and 0th term, the 1st and 2nd entity will point to the

View File

@ -7,19 +7,17 @@
namespace Drupal\system\Tests\Entity;
use Drupal\simpletest\WebTestBase;
/**
* Tests the basic Entity API.
*/
class EntityQueryTest extends WebTestBase {
class EntityQueryTest extends EntityUnitBaseTest {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('field', 'field_sql_storage', 'field_test', 'text');
public static $modules = array('field_test');
/**
* @var array
@ -55,6 +53,7 @@ class EntityQueryTest extends WebTestBase {
function setUp() {
parent::setUp();
$this->installSchema('field_test', array('test_entity', 'test_entity_revision', 'test_entity_bundle'));
$figures = drupal_strtolower($this->randomName());
$greetings = drupal_strtolower($this->randomName());
foreach (array($figures => 'shape', $greetings => 'text') as $field_name => $field_type) {

View File

@ -10,22 +10,16 @@ namespace Drupal\system\Tests\Entity;
use InvalidArgumentException;
use Drupal\Core\Language\Language;
use Drupal\simpletest\WebTestBase;
/**
* Tests entity translation.
*/
class EntityTranslationTest extends WebTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('entity_test', 'locale');
class EntityTranslationTest extends EntityUnitBaseTest {
protected $langcodes;
public static $modules = array('language', 'locale');
public static function getInfo() {
return array(
'name' => 'Entity Translation',
@ -36,6 +30,21 @@ class EntityTranslationTest extends WebTestBase {
function setUp() {
parent::setUp();
$this->installSchema('system', 'variable');
$this->installSchema('language', 'language');
$this->installSchema('entity_test', array(
'entity_test_mul',
'entity_test_mul_property_data',
'entity_test_rev',
'entity_test_rev_revision',
'entity_test_mulrev',
'entity_test_mulrev_property_data',
'entity_test_mulrev_property_revision',
));
// Create the test field.
entity_test_install();
// Enable translations for the test entity type.
state()->set('entity_test.translation', TRUE);
@ -61,6 +70,13 @@ class EntityTranslationTest extends WebTestBase {
$this->instance[$entity_type] = field_read_instance($entity_type, $this->field_name, $entity_type);
}
// 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);
}
// Create test languages.
$this->langcodes = array();
for ($i = 0; $i < 3; ++$i) {

View File

@ -8,19 +8,11 @@
namespace Drupal\system\Tests\Entity;
use Drupal\Component\Uuid\Uuid;
use Drupal\simpletest\WebTestBase;
/**
* Tests creation, saving, and loading of entity UUIDs.
*/
class EntityUUIDTest extends WebTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('entity_test');
class EntityUUIDTest extends EntityUnitBaseTest {
public static function getInfo() {
return array(
@ -30,6 +22,20 @@ class EntityUUIDTest extends WebTestBase {
);
}
public function setUp() {
parent::setUp();
$this->installSchema('entity_test', array(
'entity_test_mul',
'entity_test_mul_property_data',
'entity_test_rev',
'entity_test_rev_revision',
'entity_test_mulrev',
'entity_test_mulrev_property_data',
'entity_test_mulrev_property_revision',
));
}
/**
* Tests UUID generation in entity CRUD operations.
*/

View File

@ -0,0 +1,66 @@
<?php
/**
* @file
* Contains \Drupal\system\Tests\Entity\EntityApiTest.
*/
namespace Drupal\system\Tests\Entity;
use Drupal\simpletest\DrupalUnitTestBase;
/**
* Defines an abstract test base for entity unit tests.
*/
abstract class EntityUnitBaseTest extends DrupalUnitTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('user', 'system', 'field', 'text', 'field_sql_storage', 'entity_test');
public function setUp() {
parent::setUp();
$this->installSchema('user', 'users');
$this->installSchema('system', 'sequences');
$this->installSchema('field', array('field_config', 'field_config_instance'));
$this->installSchema('entity_test', 'entity_test');
}
/**
* Creates a user.
*
* @param array $values
* (optional) The values used to create the entity.
* @param array $permissions
* (optional) Array of permission names to assign to user. The
* role_permission and users_roles tables must be installed before this can
* be used.
*
* @return \Drupal\user\Plugin\Core\Entity\User
* The created user entity.
*/
protected function createUser($values = array(), $permissions = array()) {
if ($permissions) {
// Create a new role and apply permissions to it.
$role = entity_create('user_role', array(
'id' => strtolower($this->randomName(8)),
'label' => $this->randomName(8),
));
$role->save();
user_role_grant_permissions($role->id(), $permissions);
$values['roles'][$role->id()] = $role->id();
}
$account = entity_create('user', $values + array(
'name' => $this->randomName(),
'status' => 1,
));
$account->enforceIsNew();
$account->save();
return $account;
}
}

View File

@ -7,19 +7,10 @@
namespace Drupal\system\Tests\Entity;
use Drupal\simpletest\DrupalUnitTestBase;
/**
* Tests the basic Entity API.
*/
class EntityUriTest extends DrupalUnitTestBase {
/**
* Modules to load.
*
* @var array
*/
public static $modules = array('field', 'field_sql_storage', 'system', 'text', 'entity_test');
class EntityUriTest extends EntityUnitBaseTest {
public static function getInfo() {
return array(
@ -29,12 +20,9 @@ class EntityUriTest extends DrupalUnitTestBase {
);
}
protected function setUp() {
public function setUp() {
parent::setUp();
$this->installSchema('system', array('variable', 'url_alias'));
$this->installSchema('field', array('field_config', 'field_config_instance'));
$this->installSchema('entity_test', array('entity_test'));
}
/**

View File

@ -251,3 +251,20 @@ function entity_test_entity_test_insert($entity) {
throw new Exception("Test exception rollback.");
}
}
/**
* Entity label callback.
*
* @param $entity_type
* The entity type.
* @param $entity
* The entity object.
* @param $langcocde
* (optional) The langcode.
*
* @return
* The label of the entity prefixed with "label callback".
*/
function entity_test_label_callback($entity_type, $entity, $langcode = NULL) {
return 'label callback ' . $entity->name->value;
}

View File

@ -79,6 +79,12 @@ class EntityTest extends EntityNG {
* Overrides Drupal\entity\Entity::label().
*/
public function label($langcode = LANGUAGE_DEFAULT) {
return $this->getTranslation($langcode)->name->value;
$info = $this->entityInfo();
if (isset($info['entity_keys']['label']) && $info['entity_keys']['label'] == 'name') {
return $this->getTranslation($langcode)->name->value;
}
else {
return parent::label($langcode);
}
}
}

View File

@ -0,0 +1,30 @@
<?php
/**
* @file
* Contains \Drupal\entity_test\Plugin\Core\Entity\EntityTestLabel.
*/
namespace Drupal\entity_test\Plugin\Core\Entity;
use Drupal\Core\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;
/**
* Test entity class.
*
* @Plugin(
* id = "entity_test_label",
* label = @Translation("Entity Test label"),
* module = "entity_test",
* controller_class = "Drupal\entity_test\EntityTestStorageController",
* base_table = "entity_test",
* entity_keys = {
* "id" = "id",
* "label" = "name"
* }
* )
*/
class EntityTestLabel extends EntityTest {
}

View File

@ -0,0 +1,33 @@
<?php
/**
* @file
* Contains \Drupal\entity_test\Plugin\Core\Entity\EntityTestLabelCallback.
*/
namespace Drupal\entity_test\Plugin\Core\Entity;
use Drupal\Core\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;
/**
* Test entity class.
*
* @Plugin(
* id = "entity_test_label_callback",
* label = @Translation("Entity test label callback"),
* module = "entity_test",
* controller_class = "Drupal\entity_test\EntityTestStorageController",
* field_cache = FALSE,
* base_table = "entity_test",
* revision_table = "entity_test_revision",
* label_callback = "entity_test_label_callback",
* fieldable = TRUE,
* entity_keys = {
* "id" = "id"
* }
* )
*/
class EntityTestLabelCallback extends EntityTest {
}

View File

@ -0,0 +1,30 @@
<?php
/**
* @file
* Contains \Drupal\entity_test\Plugin\Core\Entity\EntityTestNoLabel.
*/
namespace Drupal\entity_test\Plugin\Core\Entity;
use Drupal\Core\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;
/**
* Test entity class.
*
* @Plugin(
* id = "entity_test_no_label",
* label = @Translation("Entity Test without label"),
* module = "entity_test",
* controller_class = "Drupal\entity_test\EntityTestStorageController",
* field_cache = FALSE,
* base_table = "entity_test",
* entity_keys = {
* "id" = "ftid",
* }
* )
*/
class EntityTestNoLabel extends EntityTest {
}