From 282f6b90ba5756337fdbc0782ef6442ac46214a2 Mon Sep 17 00:00:00 2001 From: webchick Date: Sat, 9 Mar 2013 22:49:53 -0800 Subject: [PATCH] Issue #1932382 by Berdir, swentel: Use DrupalUnitTestBase for Field API tests. --- .../lib/Drupal/email/Tests/EmailItemTest.php | 4 +- .../lib/Drupal/field/Tests/ActiveTest.php | 89 +++++++ .../lib/Drupal/field/Tests/BulkDeleteTest.php | 9 +- .../field/lib/Drupal/field/Tests/CrudTest.php | 67 +---- .../lib/Drupal/field/Tests/DisplayApiTest.php | 29 +-- .../field/Tests/FieldAttachOtherTest.php | 7 +- .../field/Tests/FieldAttachStorageTest.php | 7 +- .../field/Tests/FieldAttachTestBase.php | 62 ----- .../lib/Drupal/field/Tests/FieldInfoTest.php | 9 +- .../field/Tests/FieldInstanceCrudTest.php | 9 +- .../field/Tests/FieldItemUnitTestBase.php | 32 --- .../Drupal/field/Tests/FieldUnitTestBase.php | 228 ++++++++++++++++++ .../lib/Drupal/field/Tests/ShapeItemTest.php | 2 +- .../lib/Drupal/field/Tests/TestItemTest.php | 2 +- .../Drupal/field/Tests/TranslationTest.php | 53 +--- .../Drupal/field/Tests/TranslationWebTest.php | 109 +++++++++ .../Tests/FieldSqlStorageTest.php | 7 +- .../lib/Drupal/file/Tests/FileItemTest.php | 4 +- .../Drupal/number/Tests/NumberItemTest.php | 4 +- .../Drupal/options/Tests/OptionsFieldTest.php | 8 +- .../Tests/TaxonomyTermReferenceItemTest.php | 4 +- .../telephone/Tests/TelephoneItemTest.php | 4 +- 22 files changed, 479 insertions(+), 270 deletions(-) create mode 100644 core/modules/field/lib/Drupal/field/Tests/ActiveTest.php delete mode 100644 core/modules/field/lib/Drupal/field/Tests/FieldAttachTestBase.php delete mode 100644 core/modules/field/lib/Drupal/field/Tests/FieldItemUnitTestBase.php create mode 100644 core/modules/field/lib/Drupal/field/Tests/FieldUnitTestBase.php create mode 100644 core/modules/field/lib/Drupal/field/Tests/TranslationWebTest.php diff --git a/core/modules/email/lib/Drupal/email/Tests/EmailItemTest.php b/core/modules/email/lib/Drupal/email/Tests/EmailItemTest.php index a402eaca707..def99f1ba52 100644 --- a/core/modules/email/lib/Drupal/email/Tests/EmailItemTest.php +++ b/core/modules/email/lib/Drupal/email/Tests/EmailItemTest.php @@ -9,12 +9,12 @@ namespace Drupal\email\Tests; 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. diff --git a/core/modules/field/lib/Drupal/field/Tests/ActiveTest.php b/core/modules/field/lib/Drupal/field/Tests/ActiveTest.php new file mode 100644 index 00000000000..c555dc831d9 --- /dev/null +++ b/core/modules/field/lib/Drupal/field/Tests/ActiveTest.php @@ -0,0 +1,89 @@ + '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.'); + } +} diff --git a/core/modules/field/lib/Drupal/field/Tests/BulkDeleteTest.php b/core/modules/field/lib/Drupal/field/Tests/BulkDeleteTest.php index c3659619261..0635f387fda 100644 --- a/core/modules/field/lib/Drupal/field/Tests/BulkDeleteTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/BulkDeleteTest.php @@ -10,14 +10,7 @@ namespace Drupal\field\Tests; /** * 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; diff --git a/core/modules/field/lib/Drupal/field/Tests/CrudTest.php b/core/modules/field/lib/Drupal/field/Tests/CrudTest.php index 1c539daaa26..f7c3a9c9aa4 100644 --- a/core/modules/field/lib/Drupal/field/Tests/CrudTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/CrudTest.php @@ -10,14 +10,14 @@ namespace Drupal\field\Tests; 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 @@ class CrudTest extends FieldTestBase { $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.'); - } } diff --git a/core/modules/field/lib/Drupal/field/Tests/DisplayApiTest.php b/core/modules/field/lib/Drupal/field/Tests/DisplayApiTest.php index 8c2872762ac..6f7d1592d11 100644 --- a/core/modules/field/lib/Drupal/field/Tests/DisplayApiTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/DisplayApiTest.php @@ -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 @@ class DisplayApiTest extends FieldTestBase { 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 @@ class DisplayApiTest extends FieldTestBase { ), ); $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 @@ class DisplayApiTest extends FieldTestBase { ); $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 @@ class DisplayApiTest extends FieldTestBase { // 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 @@ class DisplayApiTest extends FieldTestBase { // 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 @@ class DisplayApiTest extends FieldTestBase { 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 @@ class DisplayApiTest extends FieldTestBase { 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 @@ class DisplayApiTest extends FieldTestBase { 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 @@ class DisplayApiTest extends FieldTestBase { 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 @@ class DisplayApiTest extends FieldTestBase { 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))); } } diff --git a/core/modules/field/lib/Drupal/field/Tests/FieldAttachOtherTest.php b/core/modules/field/lib/Drupal/field/Tests/FieldAttachOtherTest.php index 89e07c61da2..1ec87cfc313 100644 --- a/core/modules/field/lib/Drupal/field/Tests/FieldAttachOtherTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/FieldAttachOtherTest.php @@ -12,7 +12,7 @@ use Drupal\field\FieldValidationException; /** * 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 @@ class FieldAttachOtherTest extends FieldAttachTestBase { ); } + public function setUp() { + parent::setUp(); + $this->createFieldWithInstance(); + } + /** * Test field_attach_view() and field_attach_prepare_view(). */ diff --git a/core/modules/field/lib/Drupal/field/Tests/FieldAttachStorageTest.php b/core/modules/field/lib/Drupal/field/Tests/FieldAttachStorageTest.php index 5ae36108a80..6c7df448fad 100644 --- a/core/modules/field/lib/Drupal/field/Tests/FieldAttachStorageTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/FieldAttachStorageTest.php @@ -13,7 +13,7 @@ namespace Drupal\field\Tests; * 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 @@ class FieldAttachStorageTest extends FieldAttachTestBase { ); } + public function setUp() { + parent::setUp(); + $this->createFieldWithInstance(); + } + /** * Check field values insert, update and load. * diff --git a/core/modules/field/lib/Drupal/field/Tests/FieldAttachTestBase.php b/core/modules/field/lib/Drupal/field/Tests/FieldAttachTestBase.php deleted file mode 100644 index 9d15ba2f8d8..00000000000 --- a/core/modules/field/lib/Drupal/field/Tests/FieldAttachTestBase.php +++ /dev/null @@ -1,62 +0,0 @@ -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); - } -} diff --git a/core/modules/field/lib/Drupal/field/Tests/FieldInfoTest.php b/core/modules/field/lib/Drupal/field/Tests/FieldInfoTest.php index 3115851b950..ea6f26eb06a 100644 --- a/core/modules/field/lib/Drupal/field/Tests/FieldInfoTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/FieldInfoTest.php @@ -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( diff --git a/core/modules/field/lib/Drupal/field/Tests/FieldInstanceCrudTest.php b/core/modules/field/lib/Drupal/field/Tests/FieldInstanceCrudTest.php index 90fcc051080..ee30cf16210 100644 --- a/core/modules/field/lib/Drupal/field/Tests/FieldInstanceCrudTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/FieldInstanceCrudTest.php @@ -9,14 +9,7 @@ namespace Drupal\field\Tests; 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; diff --git a/core/modules/field/lib/Drupal/field/Tests/FieldItemUnitTestBase.php b/core/modules/field/lib/Drupal/field/Tests/FieldItemUnitTestBase.php deleted file mode 100644 index e201fb57f84..00000000000 --- a/core/modules/field/lib/Drupal/field/Tests/FieldItemUnitTestBase.php +++ /dev/null @@ -1,32 +0,0 @@ -installSchema('system', 'sequences'); - $this->installSchema('field', 'field_config'); - $this->installSchema('field', 'field_config_instance'); - $this->installSchema('entity_test', 'entity_test'); - } - -} diff --git a/core/modules/field/lib/Drupal/field/Tests/FieldUnitTestBase.php b/core/modules/field/lib/Drupal/field/Tests/FieldUnitTestBase.php new file mode 100644 index 00000000000..94b068df2e4 --- /dev/null +++ b/core/modules/field/lib/Drupal/field/Tests/FieldUnitTestBase.php @@ -0,0 +1,228 @@ +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)); + } + return $this->assert(strpos(filter_xss($this->content, array()), $text) !== FALSE, $message, $group); + } + + /** + * Pass if the text IS NOT 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 assertNoText($text, $message = '', $group = 'Other') { + if (!$message) { + $message = t('Raw "@raw" not found', array('@raw' => $text)); + } + return $this->assert(strpos(filter_xss($this->content, array()), $text) === FALSE, $message, $group); + } +} diff --git a/core/modules/field/lib/Drupal/field/Tests/ShapeItemTest.php b/core/modules/field/lib/Drupal/field/Tests/ShapeItemTest.php index c5e52c1b7e0..26282564eb3 100644 --- a/core/modules/field/lib/Drupal/field/Tests/ShapeItemTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/ShapeItemTest.php @@ -13,7 +13,7 @@ use Drupal\Core\Entity\Field\FieldInterface; /** * Tests the new entity API for the shape field type. */ -class ShapeItemTest extends FieldItemUnitTestBase { +class ShapeItemTest extends FieldUnitTestBase { /** * Modules to enable. diff --git a/core/modules/field/lib/Drupal/field/Tests/TestItemTest.php b/core/modules/field/lib/Drupal/field/Tests/TestItemTest.php index 4873eb73577..198be2af44c 100644 --- a/core/modules/field/lib/Drupal/field/Tests/TestItemTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/TestItemTest.php @@ -13,7 +13,7 @@ use Drupal\Core\Entity\Field\FieldInterface; /** * Tests the new entity API for the test field type. */ -class TestItemTest extends FieldItemUnitTestBase { +class TestItemTest extends FieldUnitTestBase { /** * Modules to enable. diff --git a/core/modules/field/lib/Drupal/field/Tests/TranslationTest.php b/core/modules/field/lib/Drupal/field/Tests/TranslationTest.php index f2ada97de47..609d70fb5d7 100644 --- a/core/modules/field/lib/Drupal/field/Tests/TranslationTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/TranslationTest.php @@ -15,14 +15,16 @@ use Drupal\Core\Language\Language; * The following tests will check the multilanguage logic of _field_invoke() and * that only the correct values are returned by field_available_languages(). */ -class TranslationTest extends FieldTestBase { +class TranslationTest extends FieldUnitTestBase { /** * Modules to enable. * + * node is required because the tests alter node entity info. + * * @var array */ - public static $modules = array('language', 'field_test'); + public static $modules = array('language', 'node'); public static function getInfo() { return array( @@ -34,6 +36,8 @@ class TranslationTest extends FieldTestBase { function setUp() { parent::setUp(); + $this->installSchema('language', array('language')); + $this->installSchema('node', array('node_type')); $this->field_name = drupal_strtolower($this->randomName() . '_field_name'); @@ -382,49 +386,4 @@ class TranslationTest extends FieldTestBase { $display_langcode = field_language($entity, $this->field_name, $requested_langcode); $this->assertEqual($display_langcode, $requested_langcode, 'Display language behave correctly when language fallback is disabled'); } - - /** - * Tests field translations when creating a new revision. - */ - function testFieldFormTranslationRevisions() { - $web_user = $this->drupalCreateUser(array('access field_test content', 'administer field_test content')); - $this->drupalLogin($web_user); - - // Prepare the field translations. - field_test_entity_info_translatable($this->entity_type, TRUE); - $eid = 1; - $entity = field_test_create_entity($eid, $eid, $this->instance['bundle']); - $available_langcodes = array_flip(field_available_languages($this->entity_type, $this->field)); - unset($available_langcodes[LANGUAGE_NOT_SPECIFIED]); - $field_name = $this->field['field_name']; - - // Store the field translations. - $entity->enforceIsNew(); - foreach ($available_langcodes as $langcode => $value) { - $entity->{$field_name}[$langcode][0]['value'] = $value + 1; - } - field_test_entity_save($entity); - - // Create a new revision. - $langcode = field_valid_language(NULL); - $edit = array("{$field_name}[$langcode][0][value]" => $entity->{$field_name}[$langcode][0]['value'], 'revision' => TRUE); - $this->drupalPost('test-entity/manage/' . $eid . '/edit', $edit, t('Save')); - - // Check translation revisions. - $this->checkTranslationRevisions($eid, $eid, $available_langcodes); - $this->checkTranslationRevisions($eid, $eid + 1, $available_langcodes); - } - - /** - * Check if the field translation attached to the entity revision identified - * by the passed arguments were correctly stored. - */ - private function checkTranslationRevisions($eid, $evid, $available_langcodes) { - $field_name = $this->field['field_name']; - $entity = field_test_entity_test_load($eid, $evid); - foreach ($available_langcodes as $langcode => $value) { - $passed = isset($entity->{$field_name}[$langcode]) && $entity->{$field_name}[$langcode][0]['value'] == $value + 1; - $this->assertTrue($passed, format_string('The @language translation for revision @revision was correctly stored', array('@language' => $langcode, '@revision' => $entity->ftvid))); - } - } } diff --git a/core/modules/field/lib/Drupal/field/Tests/TranslationWebTest.php b/core/modules/field/lib/Drupal/field/Tests/TranslationWebTest.php new file mode 100644 index 00000000000..40447f13982 --- /dev/null +++ b/core/modules/field/lib/Drupal/field/Tests/TranslationWebTest.php @@ -0,0 +1,109 @@ + 'Field translations web tests', + 'description' => 'Test multilanguage fields logic that require a full environment.', + 'group' => 'Field API', + ); + } + + function setUp() { + parent::setUp(); + + $this->field_name = drupal_strtolower($this->randomName() . '_field_name'); + + $this->entity_type = 'test_entity'; + + $field = array( + 'field_name' => $this->field_name, + 'type' => 'test_field', + 'cardinality' => 4, + 'translatable' => TRUE, + ); + field_create_field($field); + $this->field = field_read_field($this->field_name); + + $instance = array( + 'field_name' => $this->field_name, + 'entity_type' => $this->entity_type, + 'bundle' => 'test_bundle', + ); + field_create_instance($instance); + $this->instance = field_read_instance('test_entity', $this->field_name, 'test_bundle'); + + for ($i = 0; $i < 3; ++$i) { + $language = new Language(array( + 'langcode' => 'l' . $i, + 'name' => $this->randomString(), + )); + language_save($language); + } + } + + /** + * Tests field translations when creating a new revision. + */ + function testFieldFormTranslationRevisions() { + $web_user = $this->drupalCreateUser(array('access field_test content', 'administer field_test content')); + $this->drupalLogin($web_user); + + // Prepare the field translations. + field_test_entity_info_translatable($this->entity_type, TRUE); + $eid = 1; + $entity = field_test_create_entity($eid, $eid, $this->instance['bundle']); + $available_langcodes = array_flip(field_available_languages($this->entity_type, $this->field)); + unset($available_langcodes[LANGUAGE_NOT_SPECIFIED]); + $field_name = $this->field['field_name']; + + // Store the field translations. + $entity->enforceIsNew(); + foreach ($available_langcodes as $langcode => $value) { + $entity->{$field_name}[$langcode][0]['value'] = $value + 1; + } + field_test_entity_save($entity); + + // Create a new revision. + $langcode = field_valid_language(NULL); + $edit = array("{$field_name}[$langcode][0][value]" => $entity->{$field_name}[$langcode][0]['value'], 'revision' => TRUE); + $this->drupalPost('test-entity/manage/' . $eid . '/edit', $edit, t('Save')); + + // Check translation revisions. + $this->checkTranslationRevisions($eid, $eid, $available_langcodes); + $this->checkTranslationRevisions($eid, $eid + 1, $available_langcodes); + } + + /** + * Check if the field translation attached to the entity revision identified + * by the passed arguments were correctly stored. + */ + private function checkTranslationRevisions($eid, $evid, $available_langcodes) { + $field_name = $this->field['field_name']; + $entity = field_test_entity_test_load($eid, $evid); + foreach ($available_langcodes as $langcode => $value) { + $passed = isset($entity->{$field_name}[$langcode]) && $entity->{$field_name}[$langcode][0]['value'] == $value + 1; + $this->assertTrue($passed, format_string('The @language translation for revision @revision was correctly stored', array('@language' => $langcode, '@revision' => $entity->ftvid))); + } + } +} diff --git a/core/modules/field_sql_storage/lib/Drupal/field_sql_storage/Tests/FieldSqlStorageTest.php b/core/modules/field_sql_storage/lib/Drupal/field_sql_storage/Tests/FieldSqlStorageTest.php index e79757e792e..d54fd0c5c9f 100644 --- a/core/modules/field_sql_storage/lib/Drupal/field_sql_storage/Tests/FieldSqlStorageTest.php +++ b/core/modules/field_sql_storage/lib/Drupal/field_sql_storage/Tests/FieldSqlStorageTest.php @@ -9,7 +9,7 @@ namespace Drupal\field_sql_storage\Tests; use Drupal\Core\Database\Database; use Drupal\field\FieldException; -use Drupal\simpletest\WebTestBase; +use Drupal\simpletest\DrupalUnitTestBase; use Exception; use PDO; /** @@ -18,14 +18,14 @@ use PDO; * Field_sql_storage.module implements the default back-end storage plugin * for the Field Strage API. */ -class FieldSqlStorageTest extends WebTestBase { +class FieldSqlStorageTest extends \Drupal\system\Tests\Entity\EntityUnitBaseTest { /** * Modules to enable. * * @var array */ - public static $modules = array('field_sql_storage', 'field', 'field_test', 'text', 'number'); + public static $modules = array('field_test', 'text', 'number'); public static function getInfo() { return array( @@ -37,6 +37,7 @@ class FieldSqlStorageTest extends WebTestBase { function setUp() { parent::setUp(); + $this->installSchema('field_test', array('test_entity', 'test_entity_revision', 'test_entity_bundle')); $this->field_name = strtolower($this->randomName()); $this->field = array('field_name' => $this->field_name, 'type' => 'test_field', 'cardinality' => 4); diff --git a/core/modules/file/lib/Drupal/file/Tests/FileItemTest.php b/core/modules/file/lib/Drupal/file/Tests/FileItemTest.php index be275f1df15..1e6eef506a1 100644 --- a/core/modules/file/lib/Drupal/file/Tests/FileItemTest.php +++ b/core/modules/file/lib/Drupal/file/Tests/FileItemTest.php @@ -9,12 +9,12 @@ namespace Drupal\file\Tests; use Drupal\Core\Entity\Field\FieldItemInterface; use Drupal\Core\Entity\Field\FieldInterface; -use Drupal\field\Tests\FieldItemUnitTestBase; +use Drupal\field\Tests\FieldUnitTestBase; /** * Tests the new entity API for the file field type. */ -class FileItemTest extends FieldItemUnitTestBase { +class FileItemTest extends FieldUnitTestBase { /** * Modules to enable. diff --git a/core/modules/number/lib/Drupal/number/Tests/NumberItemTest.php b/core/modules/number/lib/Drupal/number/Tests/NumberItemTest.php index 0de9593df7d..a0fc77849fe 100644 --- a/core/modules/number/lib/Drupal/number/Tests/NumberItemTest.php +++ b/core/modules/number/lib/Drupal/number/Tests/NumberItemTest.php @@ -9,12 +9,12 @@ namespace Drupal\number\Tests; use Drupal\Core\Entity\Field\FieldItemInterface; use Drupal\Core\Entity\Field\FieldInterface; -use Drupal\field\Tests\FieldItemUnitTestBase; +use Drupal\field\Tests\FieldUnitTestBase; /** * Tests the new entity API for the number field type. */ -class NumberItemTest extends FieldItemUnitTestBase { +class NumberItemTest extends FieldUnitTestBase { /** * Modules to enable. diff --git a/core/modules/options/lib/Drupal/options/Tests/OptionsFieldTest.php b/core/modules/options/lib/Drupal/options/Tests/OptionsFieldTest.php index e715f550455..ca9d605067e 100644 --- a/core/modules/options/lib/Drupal/options/Tests/OptionsFieldTest.php +++ b/core/modules/options/lib/Drupal/options/Tests/OptionsFieldTest.php @@ -8,19 +8,19 @@ namespace Drupal\options\Tests; use Drupal\field\FieldException; -use Drupal\field\Tests\FieldItemUnitTestBase; +use Drupal\field\Tests\FieldUnitTestBase; /** * Tests for the 'Options' field types. */ -class OptionsFieldTest extends FieldItemUnitTestBase { +class OptionsFieldTest extends FieldUnitTestBase { /** * Modules to enable. * * @var array */ - public static $modules = array('options', 'entity_test'); + public static $modules = array('options'); public static function getInfo() { return array( @@ -32,9 +32,7 @@ class OptionsFieldTest extends FieldItemUnitTestBase { function setUp() { parent::setUp(); - $this->installSchema('system', 'menu_router'); - $this->installSchema('system', 'variable'); $this->field_name = 'test_options'; diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TaxonomyTermReferenceItemTest.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TaxonomyTermReferenceItemTest.php index f4b0ec8121d..ea9bc3eb241 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TaxonomyTermReferenceItemTest.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TaxonomyTermReferenceItemTest.php @@ -9,12 +9,12 @@ namespace Drupal\taxonomy\Tests; 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 taxonomy term reference field type. */ -class TaxonomyTermReferenceItemTest extends FieldItemUnitTestBase { +class TaxonomyTermReferenceItemTest extends FieldUnitTestBase { /** * Modules to enable. diff --git a/core/modules/telephone/lib/Drupal/telephone/Tests/TelephoneItemTest.php b/core/modules/telephone/lib/Drupal/telephone/Tests/TelephoneItemTest.php index 36dc8a9c84b..35012b3fb2e 100644 --- a/core/modules/telephone/lib/Drupal/telephone/Tests/TelephoneItemTest.php +++ b/core/modules/telephone/lib/Drupal/telephone/Tests/TelephoneItemTest.php @@ -9,12 +9,12 @@ namespace Drupal\telephone\Tests; 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 telephone field type. */ -class TelephoneItemTest extends FieldItemUnitTestBase { +class TelephoneItemTest extends FieldUnitTestBase { /** * Modules to enable.