Commit d4c21cd9 authored by webchick's avatar webchick

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

parent aab2a8cd
......@@ -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().
*/
......
<?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 {
}
<?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 {
}
<?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 {
}
......@@ -9,14 +9,15 @@
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 @@ public static function getInfo() {
);
}
/**
* 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 @@ public static function getInfo() {
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 @@ function assertEntityAccess($ops, AccessibleInterface $object, User $account = N
*/
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 @@ function testEntityAccessDefaultController() {
* 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) {
......
......@@ -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 @@ public static function getInfo() {
);
}
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 @@ public function testCRUD() {
* @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();
......
......@@ -7,7 +7,6 @@
namespace Drupal\system\Tests\Entity;
use Drupal\simpletest\WebTestBase;
use Drupal\Core\Database\Database;
/**
......@@ -22,14 +21,14 @@
* 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 @@ public static function getInfo() {
);
}
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 @@ public function testBlockHooks() {
* 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 @@ public function testCommentHooks() {
'cid' => NULL,
'pid' => 0,
'nid' => $nid,
'uid' => 1,
'uid' => $account->uid,
'subject' => 'Test comment',
'created' => REQUEST_TIME,
'changed' => REQUEST_TIME,
......@@ -202,6 +210,7 @@ public function testCommentHooks() {
* 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 @@ public function testFileHooks() {
* 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 @@ public function testNodeHooks() {
* 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 @@ public function testTaxonomyTermHooks() {
* 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',
......
......@@ -11,19 +11,18 @@
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 @@ public static function getInfo() {
);
}
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 @@ public static function getInfo() {
*/
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 @@ protected function assertReadWrite($entity_type) {
$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 @@ protected function assertReadWrite($entity_type) {
// 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 @@ public function testEntityConstraintValidation() {
$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);
......
......@@ -2,27 +2,20 @@
/**
* @file
* Definition of Drupal\field\Tests\EntityPropertiesTest.
* Contains \Drupal\system\Tests\Entity\EntityLabelTest.
*/
namespace Drupal\field\Tests;
namespace Drupal\system\Tests\Entity;
/**
* Tests entity properties.
*/
class EntityPropertiesTest extends FieldTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('field_test');
class EntityLabelTest extends EntityUnitBaseTest {
public static function getInfo() {
return array(
'name' => 'Entity properties',
'description' => 'Tests entity properties.',
'name' => 'Entity label',
'description' => 'Tests entity labels.',
'group' => 'Entity API',
);
}
......@@ -32,31 +25,29 @@ public static function getInfo() {
*/
function testEntityLabel() {
$entity_types = array(
'test_entity_no_label',
'test_entity_label',
'test_entity_label_callback',
'entity_test_no_label',
'entity_test_label',
'entity_test_label_callback',
);
// @todo Remove once test_entity entity has been merged with entity_test.
$values = array(
'ftlabel' => $this->randomName(),
'name' => $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':
case 'entity_test_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.');
case 'entity_test_label':
$this->assertEqual($label, $entity->name->value, '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.');
case 'entity_test_label_callback':
$this->assertEqual($label, 'label callback ' . $entity->name->value, 'Entity with label callback returned correct label.');
break;
}
}
......
......@@ -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 @@ public static function getInfo() {
);
}
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 @@ protected function setUp() {
));
$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
......