Issue #1932382 by Berdir, swentel: Use DrupalUnitTestBase for Field API tests.
parent
80fd0f970c
commit
282f6b90ba
|
@ -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.
|
||||
|
|
|
@ -0,0 +1,89 @@
|
|||
<?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 @@ 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;
|
||||
|
||||
|
|
|
@ -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.');
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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().
|
||||
*/
|
||||
|
|
|
@ -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.
|
||||
*
|
||||
|
|
|
@ -1,62 +0,0 @@
|
|||
<?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 @@ 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;
|
||||
|
||||
|
|
|
@ -1,32 +0,0 @@
|
|||
<?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');
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,228 @@
|
|||
<?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));
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
|
@ -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.
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,109 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Definition of Drupal\field\Tests\TranslationWebTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\field\Tests;
|
||||
|
||||
use Drupal\Core\Language\Language;
|
||||
|
||||
/**
|
||||
* Web test class for the multilanguage fields logic.
|
||||
*/
|
||||
class TranslationWebTest extends FieldTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('language', 'field_test');
|
||||
|
||||
public static function getInfo() {
|
||||
return array(
|
||||
'name' => '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)));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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';
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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.
|
||||
|
|
Loading…
Reference in New Issue