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) { ...@@ -186,23 +186,6 @@ function field_test_field_create_field($field) {
field_test_memorize(__FUNCTION__, $args); 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(). * 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 @@ ...@@ -9,14 +9,15 @@
use Drupal\Core\Language\Language; use Drupal\Core\Language\Language;
use Drupal\Core\TypedData\AccessibleInterface; use Drupal\Core\TypedData\AccessibleInterface;
use Drupal\simpletest\WebTestBase;
use Drupal\user\Plugin\Core\Entity\User; use Drupal\user\Plugin\Core\Entity\User;
use Drupal\Core\Entity\EntityAccessController; use Drupal\Core\Entity\EntityAccessController;
/** /**
* Tests the entity access controller. * Tests the entity access controller.
*/ */
class EntityAccessTest extends WebTestBase { class EntityAccessTest extends EntityUnitBaseTest {
public static $modules = array('language', 'locale');
public static function getInfo() { public static function getInfo() {
return array( return array(
...@@ -26,12 +27,20 @@ public static function getInfo() { ...@@ -26,12 +27,20 @@ public static function getInfo() {
); );
} }
/** function setUp() {
* Modules to enable. parent::setUp();
* $this->installSchema('user', array('role_permission', 'users_roles'));
* @var array $this->installSchema('system', array('variable', 'url_alias'));
*/ $this->installSchema('language', 'language');
public static $modules = array('entity_test');
// 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. * Asserts entity access correctly grants or denies access.
...@@ -39,7 +48,7 @@ public static function getInfo() { ...@@ -39,7 +48,7 @@ public static function getInfo() {
function assertEntityAccess($ops, AccessibleInterface $object, User $account = NULL) { function assertEntityAccess($ops, AccessibleInterface $object, User $account = NULL) {
foreach ($ops as $op => $result) { foreach ($ops as $op => $result) {
$message = format_string("Entity access returns @result with operation '@op'.", array( $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, '@op' => $op,
)); ));
...@@ -52,24 +61,22 @@ function assertEntityAccess($ops, AccessibleInterface $object, User $account = N ...@@ -52,24 +61,22 @@ function assertEntityAccess($ops, AccessibleInterface $object, User $account = N
*/ */
function testEntityAccess() { function testEntityAccess() {
// Set up a non-admin user that is allowed to view test entities. // Set up a non-admin user that is allowed to view test entities.
$user = $this->drupalCreateUser(array('view test entity')); global $user;
$this->drupalLogin($user); $user = $this->createUser(array('uid' => 2), array('view test entity'));
$entity = entity_create('entity_test', array( $entity = entity_create('entity_test', array(
'name' => 'test', '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( $this->assertEntityAccess(array(
'create' => TRUE, 'create' => FALSE,
'update' => TRUE, 'update' => FALSE,
'delete' => TRUE, 'delete' => FALSE,
'view' => TRUE, 'view' => TRUE,
), $entity); ), $entity);
// The custom user is not allowed to perform any operation on test entities. // The custom user is not allowed to perform any operation on test entities.
$custom_user = $this->drupalCreateUser(); $custom_user = $this->createUser();
$this->assertEntityAccess(array( $this->assertEntityAccess(array(
'create' => FALSE, 'create' => FALSE,
'update' => FALSE, 'update' => FALSE,
...@@ -100,13 +107,10 @@ function testEntityAccessDefaultController() { ...@@ -100,13 +107,10 @@ function testEntityAccessDefaultController() {
* Ensures entity access for entity translations is properly working. * Ensures entity access for entity translations is properly working.
*/ */
function testEntityTranslationAccess() { 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. // Set up a non-admin user that is allowed to view test entity translations.
$user = $this->drupalCreateUser(array('view test entity translations')); global $user;
$this->drupalLogin($user); $user = $this->createUser(array('uid' => 2), array('view test entity translations'));
// Create two test languages. // Create two test languages.
foreach (array('foo', 'bar') as $langcode) { foreach (array('foo', 'bar') as $langcode) {
......
...@@ -7,19 +7,12 @@ ...@@ -7,19 +7,12 @@
namespace Drupal\system\Tests\Entity; namespace Drupal\system\Tests\Entity;
use Drupal\simpletest\WebTestBase; use Drupal\user\Plugin\Core\Entity\User;
/** /**
* Tests the basic Entity API. * Tests the basic Entity API.
*/ */
class EntityApiTest extends WebTestBase { class EntityApiTest extends EntityUnitBaseTest {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('entity_test');
public static function getInfo() { public static function getInfo() {
return array( return array(
...@@ -29,15 +22,26 @@ public static function getInfo() { ...@@ -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. * Tests basic CRUD functionality of the Entity API.
*/ */
public function testCRUD() { public function testCRUD() {
$user1 = $this->drupalCreateUser();
// All entity variations have to have the same results. // All entity variations have to have the same results.
foreach (entity_test_entity_types() as $entity_type) { 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() { ...@@ -49,7 +53,7 @@ public function testCRUD() {
* @param \Drupal\user\Plugin\Core\Entity\User $user1 * @param \Drupal\user\Plugin\Core\Entity\User $user1
* The user to run the tests with. * 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. // Create some test entities.
$entity = entity_create($entity_type, array('name' => 'test', 'user_id' => $user1->uid)); $entity = entity_create($entity_type, array('name' => 'test', 'user_id' => $user1->uid));
$entity->save(); $entity->save();
......
...@@ -7,7 +7,6 @@ ...@@ -7,7 +7,6 @@
namespace Drupal\system\Tests\Entity; namespace Drupal\system\Tests\Entity;
use Drupal\simpletest\WebTestBase;
use Drupal\Core\Database\Database; use Drupal\Core\Database\Database;
/** /**
...@@ -22,14 +21,14 @@ ...@@ -22,14 +21,14 @@
* As well as all type-specific hooks, like hook_node_insert(), * As well as all type-specific hooks, like hook_node_insert(),
* hook_comment_update(), etc. * hook_comment_update(), etc.
*/ */
class EntityCrudHookTest extends WebTestBase { class EntityCrudHookTest extends EntityUnitBaseTest {
/** /**
* Modules to enable. * Modules to enable.
* *
* @var array * @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(); protected $ids = array();
...@@ -41,6 +40,13 @@ public static function getInfo() { ...@@ -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. * Checks the order of CRUD hook execution messages.
* *
...@@ -124,8 +130,10 @@ public function testBlockHooks() { ...@@ -124,8 +130,10 @@ public function testBlockHooks() {
* Tests hook invocations for CRUD operations on comments. * Tests hook invocations for CRUD operations on comments.
*/ */
public function testCommentHooks() { public function testCommentHooks() {
$account = $this->createUser();
$node = entity_create('node', array( $node = entity_create('node', array(
'uid' => 1, 'uid' => $account->uid,
'type' => 'article', 'type' => 'article',
'title' => 'Test node', 'title' => 'Test node',
'status' => 1, 'status' => 1,
...@@ -145,7 +153,7 @@ public function testCommentHooks() { ...@@ -145,7 +153,7 @@ public function testCommentHooks() {
'cid' => NULL, 'cid' => NULL,
'pid' => 0, 'pid' => 0,
'nid' => $nid, 'nid' => $nid,
'uid' => 1, 'uid' => $account->uid,
'subject' => 'Test comment', 'subject' => 'Test comment',
'created' => REQUEST_TIME, 'created' => REQUEST_TIME,
'changed' => REQUEST_TIME, 'changed' => REQUEST_TIME,
...@@ -202,6 +210,7 @@ public function testCommentHooks() { ...@@ -202,6 +210,7 @@ public function testCommentHooks() {
* Tests hook invocations for CRUD operations on files. * Tests hook invocations for CRUD operations on files.
*/ */
public function testFileHooks() { public function testFileHooks() {
$this->installSchema('file', array('file_managed', 'file_usage'));
$url = 'public://entity_crud_hook_test.file'; $url = 'public://entity_crud_hook_test.file';
file_put_contents($url, 'Test test test'); file_put_contents($url, 'Test test test');
$file = entity_create('file', array( $file = entity_create('file', array(
...@@ -264,8 +273,10 @@ public function testFileHooks() { ...@@ -264,8 +273,10 @@ public function testFileHooks() {
* Tests hook invocations for CRUD operations on nodes. * Tests hook invocations for CRUD operations on nodes.
*/ */
public function testNodeHooks() { public function testNodeHooks() {
$account = $this->createUser();
$node = entity_create('node', array( $node = entity_create('node', array(
'uid' => 1, 'uid' => $account->id(),
'type' => 'article', 'type' => 'article',
'title' => 'Test node', 'title' => 'Test node',
'status' => 1, 'status' => 1,
...@@ -326,6 +337,8 @@ public function testNodeHooks() { ...@@ -326,6 +337,8 @@ public function testNodeHooks() {
* Tests hook invocations for CRUD operations on taxonomy terms. * Tests hook invocations for CRUD operations on taxonomy terms.
*/ */
public function testTaxonomyTermHooks() { public function testTaxonomyTermHooks() {
$this->installSchema('taxonomy', array('taxonomy_term_data', 'taxonomy_term_hierarchy'));
$vocabulary = entity_create('taxonomy_vocabulary', array( $vocabulary = entity_create('taxonomy_vocabulary', array(
'name' => 'Test vocabulary', 'name' => 'Test vocabulary',
'vid' => 'test', 'vid' => 'test',
...@@ -393,6 +406,8 @@ public function testTaxonomyTermHooks() { ...@@ -393,6 +406,8 @@ public function testTaxonomyTermHooks() {
* Tests hook invocations for CRUD operations on taxonomy vocabularies. * Tests hook invocations for CRUD operations on taxonomy vocabularies.
*/ */
public function testTaxonomyVocabularyHooks() { public function testTaxonomyVocabularyHooks() {
$this->installSchema('taxonomy', array('taxonomy_term_data', 'taxonomy_term_hierarchy'));
$vocabulary = entity_create('taxonomy_vocabulary', array( $vocabulary = entity_create('taxonomy_vocabulary', array(
'name' => 'Test vocabulary', 'name' => 'Test vocabulary',
'vid' => 'test', 'vid' => 'test',
......
...@@ -11,19 +11,18 @@ ...@@ -11,19 +11,18 @@
use Drupal\Core\Entity\Field\FieldInterface; use Drupal\Core\Entity\Field\FieldInterface;
use Drupal\Core\Entity\Field\FieldItemInterface; use Drupal\Core\Entity\Field\FieldItemInterface;
use Drupal\Core\TypedData\TypedDataInterface; use Drupal\Core\TypedData\TypedDataInterface;
use Drupal\simpletest\WebTestBase;
/** /**
* Tests Entity API base functionality. * Tests Entity API base functionality.
*/ */
class EntityFieldTest extends WebTestBase { class EntityFieldTest extends EntityUnitBaseTest {
/** /**
* Modules to enable. * Modules to enable.
* *
* @var array * @var array
*/ */
public static $modules = array('entity_test'); public static $modules = array('filter', 'text', 'node');
public static function getInfo() { public static function getInfo() {
return array( return array(
...@@ -33,6 +32,27 @@ public static function getInfo() { ...@@ -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. * Creates a test entity.
* *
...@@ -40,7 +60,7 @@ public static function getInfo() { ...@@ -40,7 +60,7 @@ public static function getInfo() {
*/ */
protected function createTestEntity($entity_type) { protected function createTestEntity($entity_type) {
$this->entity_name = $this->randomName(); $this->entity_name = $this->randomName();
$this->entity_user = $this->drupalCreateUser(); $this->entity_user = $this->createUser();
$this->entity_field_text = $this->randomName(); $this->entity_field_text = $this->randomName();
// Pass in the value of the name field when creating. With the user // Pass in the value of the name field when creating. With the user
...@@ -100,13 +120,13 @@ protected function assertReadWrite($entity_type) { ...@@ -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))); $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. // Change the assigned user by entity.
$new_user = $this->drupalCreateUser(); $new_user = $this->createUser();
$entity->user_id->entity = $new_user; $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->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))); $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. // Change the assigned user by id.
$new_user = $this->drupalCreateUser(); $new_user = $this->createUser();
$entity->user_id->target_id = $new_user->uid; $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->uid, $entity->user_id->target_id, format_string('%entity_type: Updated user id can be read.', array('%entity_type' => $entity_type)));