Commit 282f6b90 authored by webchick's avatar webchick

Issue #1932382 by Berdir, swentel: Use DrupalUnitTestBase for Field API tests.

parent 80fd0f97
......@@ -9,12 +9,12 @@
use Drupal\Core\Entity\Field\FieldInterface;
use Drupal\Core\Entity\Field\FieldItemInterface;
use Drupal\field\Tests\FieldItemUnitTestBase;
use Drupal\field\Tests\FieldUnitTestBase;
/**
* Tests the new entity API for the email field type.
*/
class EmailItemTest extends FieldItemUnitTestBase {
class EmailItemTest extends FieldUnitTestBase {
/**
* Modules to enable.
......
<?php
/**
* @file
* Definition of Drupal\field\Tests\ActiveTest.
*/
namespace Drupal\field\Tests;
class ActiveTest extends FieldTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('field_test');
public static function getInfo() {
return array(
'name' => 'Field active test',
'description' => 'Test that fields are properly marked active or inactive.',
'group' => 'Field API',
);
}
/**
* Test that fields are properly marked active or inactive.
*/
function testActive() {
$field_definition = array(
'field_name' => 'field_1',
'type' => 'test_field',
// For this test, we need a storage backend provided by a different
// module than field_test.module.
'storage' => array(
'type' => 'field_sql_storage',
),
);
field_create_field($field_definition);
// Test disabling and enabling:
// - the field type module,
// - the storage module,
// - both.
$this->_testActiveHelper($field_definition, array('field_test'));
$this->_testActiveHelper($field_definition, array('field_sql_storage'));
$this->_testActiveHelper($field_definition, array('field_test', 'field_sql_storage'));
}
/**
* Helper function for testActive().
*
* Test dependency between a field and a set of modules.
*
* @param $field_definition
* A field definition.
* @param $modules
* An aray of module names. The field will be tested to be inactive as long
* as any of those modules is disabled.
*/
function _testActiveHelper($field_definition, $modules) {
$field_name = $field_definition['field_name'];
// Read the field.
$field = field_read_field($field_name);
$this->assertTrue($field_definition <= $field, 'The field was properly read.');
module_disable($modules, FALSE);
$fields = field_read_fields(array('field_name' => $field_name), array('include_inactive' => TRUE));
$this->assertTrue(isset($fields[$field_name]) && $field_definition < $field, 'The field is properly read when explicitly fetching inactive fields.');
// Re-enable modules one by one, and check that the field is still inactive
// while some modules remain disabled.
while ($modules) {
$field = field_read_field($field_name);
$this->assertTrue(empty($field), format_string('%modules disabled. The field is marked inactive.', array('%modules' => implode(', ', $modules))));
$module = array_shift($modules);
module_enable(array($module), FALSE);
}
// Check that the field is active again after all modules have been
// enabled.
$field = field_read_field($field_name);
$this->assertTrue($field_definition <= $field, 'The field was was marked active.');
}
}
......@@ -10,14 +10,7 @@
/**
* Unit test class for field bulk delete and batch purge functionality.
*/
class BulkDeleteTest extends FieldTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('field_test');
class BulkDeleteTest extends FieldUnitTestBase {
protected $field;
......
......@@ -10,14 +10,14 @@
use Drupal\field\FieldException;
use Exception;
class CrudTest extends FieldTestBase {
class CrudTest extends FieldUnitTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('field_test', 'number');
public static $modules = array('number');
public static function getInfo() {
return array(
......@@ -444,67 +444,4 @@ function testUpdateFieldForbid() {
$this->pass(t("An unchangeable setting cannot be updated."));
}
}
/**
* Test that fields are properly marked active or inactive.
*/
function testActive() {
$field_definition = array(
'field_name' => 'field_1',
'type' => 'test_field',
// For this test, we need a storage backend provided by a different
// module than field_test.module.
'storage' => array(
'type' => 'field_sql_storage',
),
);
field_create_field($field_definition);
// Test disabling and enabling:
// - the field type module,
// - the storage module,
// - both.
$this->_testActiveHelper($field_definition, array('field_test'));
$this->_testActiveHelper($field_definition, array('field_sql_storage'));
$this->_testActiveHelper($field_definition, array('field_test', 'field_sql_storage'));
}
/**
* Helper function for testActive().
*
* Test dependency between a field and a set of modules.
*
* @param $field_definition
* A field definition.
* @param $modules
* An aray of module names. The field will be tested to be inactive as long
* as any of those modules is disabled.
*/
function _testActiveHelper($field_definition, $modules) {
$field_name = $field_definition['field_name'];
// Read the field.
$field = field_read_field($field_name);
$this->assertTrue($field_definition <= $field, 'The field was properly read.');
module_disable($modules, FALSE);
$fields = field_read_fields(array('field_name' => $field_name), array('include_inactive' => TRUE));
$this->assertTrue(isset($fields[$field_name]) && $field_definition < $field, 'The field is properly read when explicitly fetching inactive fields.');
// Re-enable modules one by one, and check that the field is still inactive
// while some modules remain disabled.
while ($modules) {
$field = field_read_field($field_name);
$this->assertTrue(empty($field), format_string('%modules disabled. The field is marked inactive.', array('%modules' => implode(', ', $modules))));
$module = array_shift($modules);
module_enable(array($module), FALSE);
}
// Check that the field is active again after all modules have been
// enabled.
$field = field_read_field($field_name);
$this->assertTrue($field_definition <= $field, 'The field was was marked active.');
}
}
......@@ -7,14 +7,7 @@
namespace Drupal\field\Tests;
class DisplayApiTest extends FieldTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('field_test');
class DisplayApiTest extends FieldUnitTestBase {
public static function getInfo() {
return array(
......@@ -84,7 +77,7 @@ function setUp() {
function testFieldViewField() {
// No display settings: check that default display settings are used.
$output = field_view_field($this->entity, $this->field_name);
$this->drupalSetContent(drupal_render($output));
$this->content = drupal_render($output);
$settings = field_info_formatter_settings('field_test_default');
$setting = $settings['test_formatter_setting'];
$this->assertText($this->label, 'Label was displayed.');
......@@ -102,7 +95,7 @@ function testFieldViewField() {
),
);
$output = field_view_field($this->entity, $this->field_name, $display);
$this->drupalSetContent(drupal_render($output));
$this->content = drupal_render($output);
$setting = $display['settings']['test_formatter_setting_multiple'];
$this->assertNoText($this->label, 'Label was not displayed.');
$this->assertText('field_test_field_attach_view_alter', 'Alter fired, display passed.');
......@@ -122,7 +115,7 @@ function testFieldViewField() {
);
$output = field_view_field($this->entity, $this->field_name, $display);
$view = drupal_render($output);
$this->drupalSetContent($view);
$this->content = $view;
$setting = $display['settings']['test_formatter_setting_additional'];
$this->assertNoText($this->label, 'Label was not displayed.');
$this->assertNoText('field_test_field_attach_view_alter', 'Alter not fired.');
......@@ -133,7 +126,7 @@ function testFieldViewField() {
// View mode: check that display settings specified in the display object
// are used.
$output = field_view_field($this->entity, $this->field_name, 'teaser');
$this->drupalSetContent(drupal_render($output));
$this->content = drupal_render($output);
$setting = $this->display_options['teaser']['settings']['test_formatter_setting'];
$this->assertText($this->label, 'Label was displayed.');
foreach ($this->values as $delta => $value) {
......@@ -143,7 +136,7 @@ function testFieldViewField() {
// Unknown view mode: check that display settings for 'default' view mode
// are used.
$output = field_view_field($this->entity, $this->field_name, 'unknown_view_mode');
$this->drupalSetContent(drupal_render($output));
$this->content = drupal_render($output);
$setting = $this->display_options['default']['settings']['test_formatter_setting'];
$this->assertText($this->label, 'Label was displayed.');
foreach ($this->values as $delta => $value) {
......@@ -161,7 +154,7 @@ function testFieldViewValue() {
foreach ($this->values as $delta => $value) {
$item = $this->entity->{$this->field_name}[LANGUAGE_NOT_SPECIFIED][$delta];
$output = field_view_value($this->entity, $this->field_name, $item);
$this->drupalSetContent(drupal_render($output));
$this->content = drupal_render($output);
$this->assertText($setting . '|' . $value['value'], format_string('Value @delta was displayed with expected setting.', array('@delta' => $delta)));
}
......@@ -177,7 +170,7 @@ function testFieldViewValue() {
foreach ($this->values as $delta => $value) {
$item = $this->entity->{$this->field_name}[LANGUAGE_NOT_SPECIFIED][$delta];
$output = field_view_value($this->entity, $this->field_name, $item, $display);
$this->drupalSetContent(drupal_render($output));
$this->content = drupal_render($output);
$this->assertText($setting . '|0:' . $value['value'], format_string('Value @delta was displayed with expected setting.', array('@delta' => $delta)));
}
......@@ -193,7 +186,7 @@ function testFieldViewValue() {
foreach ($this->values as $delta => $value) {
$item = $this->entity->{$this->field_name}[LANGUAGE_NOT_SPECIFIED][$delta];
$output = field_view_value($this->entity, $this->field_name, $item, $display);
$this->drupalSetContent(drupal_render($output));
$this->content = drupal_render($output);
$this->assertText($setting . '|' . $value['value'] . '|' . ($value['value'] + 1), format_string('Value @delta was displayed with expected setting.', array('@delta' => $delta)));
}
......@@ -203,7 +196,7 @@ function testFieldViewValue() {
foreach ($this->values as $delta => $value) {
$item = $this->entity->{$this->field_name}[LANGUAGE_NOT_SPECIFIED][$delta];
$output = field_view_value($this->entity, $this->field_name, $item, 'teaser');
$this->drupalSetContent(drupal_render($output));
$this->content = drupal_render($output);
$this->assertText($setting . '|' . $value['value'], format_string('Value @delta was displayed with expected setting.', array('@delta' => $delta)));
}
......@@ -213,7 +206,7 @@ function testFieldViewValue() {
foreach ($this->values as $delta => $value) {
$item = $this->entity->{$this->field_name}[LANGUAGE_NOT_SPECIFIED][$delta];
$output = field_view_value($this->entity, $this->field_name, $item, 'unknown_view_mode');
$this->drupalSetContent(drupal_render($output));
$this->content = drupal_render($output);
$this->assertText($setting . '|' . $value['value'], format_string('Value @delta was displayed with expected setting.', array('@delta' => $delta)));
}
}
......
......@@ -12,7 +12,7 @@
/**
* Unit test class for non-storage related field_attach_* functions.
*/
class FieldAttachOtherTest extends FieldAttachTestBase {
class FieldAttachOtherTest extends FieldUnitTestBase {
public static function getInfo() {
return array(
'name' => 'Field attach tests (other)',
......@@ -21,6 +21,11 @@ public static function getInfo() {
);
}
public function setUp() {
parent::setUp();
$this->createFieldWithInstance();
}
/**
* Test field_attach_view() and field_attach_prepare_view().
*/
......
......@@ -13,7 +13,7 @@
* All field_attach_* test work with all field_storage plugins and
* all hook_field_attach_pre_{load,insert,update}() hooks.
*/
class FieldAttachStorageTest extends FieldAttachTestBase {
class FieldAttachStorageTest extends FieldUnitTestBase {
public static function getInfo() {
return array(
'name' => 'Field attach tests (storage-related)',
......@@ -22,6 +22,11 @@ public static function getInfo() {
);
}
public function setUp() {
parent::setUp();
$this->createFieldWithInstance();
}
/**
* Check field values insert, update and load.
*
......
<?php
/**
* @file
* Definition of Drupal\field\Tests\FieldAttachTestBase.
*/
namespace Drupal\field\Tests;
abstract class FieldAttachTestBase extends FieldTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('field_test');
function setUp() {
parent::setUp();
$this->createFieldWithInstance();
}
/**
* Create a field and an instance of it.
*
* @param string $suffix
* (optional) A string that should only contain characters that are valid in
* PHP variable names as well.
*/
function createFieldWithInstance($suffix = '') {
$field_name = 'field_name' . $suffix;
$field = 'field' . $suffix;
$field_id = 'field_id' . $suffix;
$instance = 'instance' . $suffix;
$this->$field_name = drupal_strtolower($this->randomName() . '_field_name' . $suffix);
$this->$field = array('field_name' => $this->$field_name, 'type' => 'test_field', 'cardinality' => 4);
$this->$field = field_create_field($this->$field);
$this->$field_id = $this->{$field}['id'];
$this->$instance = array(
'field_name' => $this->$field_name,
'entity_type' => 'test_entity',
'bundle' => 'test_bundle',
'label' => $this->randomName() . '_label',
'description' => $this->randomName() . '_description',
'weight' => mt_rand(0, 127),
'settings' => array(
'test_instance_setting' => $this->randomName(),
),
'widget' => array(
'type' => 'test_field_widget',
'label' => 'Test Field',
'settings' => array(
'test_widget_setting' => $this->randomName(),
)
)
);
field_create_instance($this->$instance);
}
}
......@@ -7,14 +7,7 @@
namespace Drupal\field\Tests;
class FieldInfoTest extends FieldTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('field_test');
class FieldInfoTest extends FieldUnitTestBase {
public static function getInfo() {
return array(
......
......@@ -9,14 +9,7 @@
use Drupal\field\FieldException;
class FieldInstanceCrudTest extends FieldTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('field_test');
class FieldInstanceCrudTest extends FieldUnitTestBase {
protected $field;
......
<?php
/**
* @file
* Contains \Drupal\field\Tests\FieldItemUnitTestBase.
*/
namespace Drupal\field\Tests;
use Drupal\simpletest\DrupalUnitTestBase;
/**
* Base test class for field type item tests.
*/
class FieldItemUnitTestBase extends DrupalUnitTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('user', 'system', 'field', 'text', 'field_sql_storage', 'field_test', 'entity_test');
public function setUp() {
parent::setUp();
$this->installSchema('system', 'sequences');
$this->installSchema('field', 'field_config');
$this->installSchema('field', 'field_config_instance');
$this->installSchema('entity_test', 'entity_test');
}
}
<?php
/**
* @file
* Contains \Drupal\field\Tests\FieldUnitTestBase.
*/
namespace Drupal\field\Tests;
use Drupal\Core\Entity\EntityInterface;
use Drupal\simpletest\DrupalUnitTestBase;
/**
* Parent class for Field API unit tests.
*/
abstract class FieldUnitTestBase extends DrupalUnitTestBase {
var $default_storage = 'field_sql_storage';
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('user', 'entity', 'system', 'field', 'text', 'field_sql_storage', 'entity_test', 'field_test');
/**
* A string for assert raw and text helper methods.
*
* @var string
*/
protected $content;
/**
* Set the default field storage backend for fields created during tests.
*/
function setUp() {
parent::setUp();
$this->installSchema('system', array('sequences', 'variable'));
$this->installSchema('field', array('field_config', 'field_config_instance'));
$this->installSchema('entity_test', 'entity_test');
$this->installSchema('field_test', array('test_entity', 'test_entity_revision', 'test_entity_bundle'));
// Set default storage backend.
variable_set('field_storage_default', $this->default_storage);
}
/**
* Create a field and an instance of it.
*
* @param string $suffix
* (optional) A string that should only contain characters that are valid in
* PHP variable names as well.
*/
function createFieldWithInstance($suffix = '') {
$field_name = 'field_name' . $suffix;
$field = 'field' . $suffix;
$field_id = 'field_id' . $suffix;
$instance = 'instance' . $suffix;
$this->$field_name = drupal_strtolower($this->randomName() . '_field_name' . $suffix);
$this->$field = array('field_name' => $this->$field_name, 'type' => 'test_field', 'cardinality' => 4);
$this->$field = field_create_field($this->$field);
$this->$field_id = $this->{$field}['id'];
$this->$instance = array(
'field_name' => $this->$field_name,
'entity_type' => 'test_entity',
'bundle' => 'test_bundle',
'label' => $this->randomName() . '_label',
'description' => $this->randomName() . '_description',
'weight' => mt_rand(0, 127),
'settings' => array(
'test_instance_setting' => $this->randomName(),
),
'widget' => array(
'type' => 'test_field_widget',
'label' => 'Test Field',
'settings' => array(
'test_widget_setting' => $this->randomName(),
)
)
);
field_create_instance($this->$instance);
}
/**
* Generate random values for a field_test field.
*
* @param $cardinality
* Number of values to generate.
* @return
* An array of random values, in the format expected for field values.
*/
function _generateTestFieldValues($cardinality) {
$values = array();
for ($i = 0; $i < $cardinality; $i++) {
// field_test fields treat 0 as 'empty value'.
$values[$i]['value'] = mt_rand(1, 127);
}
return $values;
}
/**
* Assert that a field has the expected values in an entity.
*
* This function only checks a single column in the field values.
*
* @param EntityInterface $entity
* The entity to test.
* @param $field_name
* The name of the field to test
* @param $langcode
* The language code for the values.
* @param $expected_values
* The array of expected values.
* @param $column
* (Optional) the name of the column to check.
*/
function assertFieldValues(EntityInterface $entity, $field_name, $langcode, $expected_values, $column = 'value') {
$e = clone $entity;
field_attach_load('test_entity', array($e->ftid => $e));
$values = isset($e->{$field_name}[$langcode]) ? $e->{$field_name}[$langcode] : array();
$this->assertEqual(count($values), count($expected_values), 'Expected number of values were saved.');
foreach ($expected_values as $key => $value) {
$this->assertEqual($values[$key][$column], $value, format_string('Value @value was saved correctly.', array('@value' => $value)));
}
}
/**
* Pass if the raw text IS found in set string.
*
* @param $raw
* Raw (HTML) string to look for.
* @param $message
* (optional) A message to display with the assertion. Do not translate
* messages: use format_string() to embed variables in the message text, not
* t(). If left blank, a default message will be displayed.
* @param $group
* (optional) The group this message is in, which is displayed in a column
* in test output. Use 'Debug' to indicate this is debugging output. Do not
* translate this string. Defaults to 'Other'; most tests do not override
* this default.
*
* @return
* TRUE on pass, FALSE on fail.
*/
protected function assertRaw($raw, $message = '', $group = 'Other') {
if (!$message) {
$message = t('Raw "@raw" found', array('@raw' => $raw));
}
return $this->assert(strpos($this->content, $raw) !== FALSE, $message, $group);
}
/**
* Pass if the raw text IS NOT found in set string.
*
* @param $raw
* Raw (HTML) string to look for.
* @param $message
* (optional) A message to display with the assertion. Do not translate
* messages: use format_string() to embed variables in the message text, not
* t(). If left blank, a default message will be displayed.
* @param $group
* (optional) The group this message is in, which is displayed in a column
* in test output. Use 'Debug' to indicate this is debugging output. Do not
* translate this string. Defaults to 'Other'; most tests do not override
* this default.
*
* @return
* TRUE on pass, FALSE on fail.
*/
protected function assertNoRaw($raw, $message = '', $group = 'Other') {
if (!$message) {
$message = t('Raw "@raw" found', array('@raw' => $raw));
}
return $this->assert(strpos($this->content, $raw) === FALSE, $message, $group);
}
/**
* Pass if the text IS found in set string.
*
* @param $text
* Text to look for.
* @param $message
* (optional) A message to display with the assertion. Do not translate
* messages: use format_string() to embed variables in the message text, not
* t(). If left blank, a default message will be displayed.
* @param $group
* (optional) The group this message is in, which is displayed in a column
* in test output. Use 'Debug' to indicate this is debugging output. Do not
* translate this string. Defaults to 'Other'; most tests do not override
* this default.
*
* @return
* TRUE on pass, FALSE on fail.
*/
protected function assertText($text, $message = '', $group = 'Other') {
if (!$message) {
$message = t('Raw "@raw" found', array('@raw' => $text));
}