Commit 28542bbc authored by webchick's avatar webchick

Issue #1591852 by aspilicious, Rob Loach: Convert field tests to PSR-0.

parent 3c00c301
......@@ -3,9 +3,6 @@ description = Field API to add fields to entities like nodes and users.
package = Core
version = VERSION
core = 8.x
files[] = field.module
files[] = field.attach.inc
files[] = tests/field.test
dependencies[] = field_sql_storage
dependencies[] = entity
required = TRUE
......
This diff is collapsed.
This diff is collapsed.
<?php
/**
* @file
* Definition of Drupal\field\Tests\DisplayApiTest.
*/
namespace Drupal\field\Tests;
class DisplayApiTest extends FieldTestBase {
public static function getInfo() {
return array(
'name' => 'Field Display API tests',
'description' => 'Test the display API.',
'group' => 'Field API',
);
}
function setUp() {
parent::setUp('field_test');
// Create a field and instance.
$this->field_name = 'test_field';
$this->label = $this->randomName();
$this->cardinality = 4;
$this->field = array(
'field_name' => $this->field_name,
'type' => 'test_field',
'cardinality' => $this->cardinality,
);
$this->instance = array(
'field_name' => $this->field_name,
'entity_type' => 'test_entity',
'bundle' => 'test_bundle',
'label' => $this->label,
'display' => array(
'default' => array(
'type' => 'field_test_default',
'settings' => array(
'test_formatter_setting' => $this->randomName(),
),
),
'teaser' => array(
'type' => 'field_test_default',
'settings' => array(
'test_formatter_setting' => $this->randomName(),
),
),
),
);
field_create_field($this->field);
field_create_instance($this->instance);
// Create an entity with values.
$this->values = $this->_generateTestFieldValues($this->cardinality);
$this->entity = field_test_create_stub_entity();
$this->is_new = TRUE;
$this->entity->{$this->field_name}[LANGUAGE_NOT_SPECIFIED] = $this->values;
field_test_entity_save($this->entity);
}
/**
* Test the field_view_field() function.
*/
function testFieldViewField() {
// No display settings: check that default display settings are used.
$output = field_view_field('test_entity', $this->entity, $this->field_name);
$this->drupalSetContent(drupal_render($output));
$settings = field_info_formatter_settings('field_test_default');
$setting = $settings['test_formatter_setting'];
$this->assertText($this->label, t('Label was displayed.'));
foreach ($this->values as $delta => $value) {
$this->assertText($setting . '|' . $value['value'], t('Value @delta was displayed with expected setting.', array('@delta' => $delta)));
}
// Check that explicit display settings are used.
$display = array(
'label' => 'hidden',
'type' => 'field_test_multiple',
'settings' => array(
'test_formatter_setting_multiple' => $this->randomName(),
'alter' => TRUE,
),
);
$output = field_view_field('test_entity', $this->entity, $this->field_name, $display);
$this->drupalSetContent(drupal_render($output));
$setting = $display['settings']['test_formatter_setting_multiple'];
$this->assertNoText($this->label, t('Label was not displayed.'));
$this->assertText('field_test_field_attach_view_alter', t('Alter fired, display passed.'));
$array = array();
foreach ($this->values as $delta => $value) {
$array[] = $delta . ':' . $value['value'];
}
$this->assertText($setting . '|' . implode('|', $array), t('Values were displayed with expected setting.'));
// Check the prepare_view steps are invoked.
$display = array(
'label' => 'hidden',
'type' => 'field_test_with_prepare_view',
'settings' => array(
'test_formatter_setting_additional' => $this->randomName(),
),
);
$output = field_view_field('test_entity', $this->entity, $this->field_name, $display);
$view = drupal_render($output);
$this->drupalSetContent($view);
$setting = $display['settings']['test_formatter_setting_additional'];
$this->assertNoText($this->label, t('Label was not displayed.'));
$this->assertNoText('field_test_field_attach_view_alter', t('Alter not fired.'));
foreach ($this->values as $delta => $value) {
$this->assertText($setting . '|' . $value['value'] . '|' . ($value['value'] + 1), t('Value @delta was displayed with expected setting.', array('@delta' => $delta)));
}
// View mode: check that display settings specified in the instance are
// used.
$output = field_view_field('test_entity', $this->entity, $this->field_name, 'teaser');
$this->drupalSetContent(drupal_render($output));
$setting = $this->instance['display']['teaser']['settings']['test_formatter_setting'];
$this->assertText($this->label, t('Label was displayed.'));
foreach ($this->values as $delta => $value) {
$this->assertText($setting . '|' . $value['value'], t('Value @delta was displayed with expected setting.', array('@delta' => $delta)));
}
// Unknown view mode: check that display settings for 'default' view mode
// are used.
$output = field_view_field('test_entity', $this->entity, $this->field_name, 'unknown_view_mode');
$this->drupalSetContent(drupal_render($output));
$setting = $this->instance['display']['default']['settings']['test_formatter_setting'];
$this->assertText($this->label, t('Label was displayed.'));
foreach ($this->values as $delta => $value) {
$this->assertText($setting . '|' . $value['value'], t('Value @delta was displayed with expected setting.', array('@delta' => $delta)));
}
}
/**
* Test the field_view_value() function.
*/
function testFieldViewValue() {
// No display settings: check that default display settings are used.
$settings = field_info_formatter_settings('field_test_default');
$setting = $settings['test_formatter_setting'];
foreach ($this->values as $delta => $value) {
$item = $this->entity->{$this->field_name}[LANGUAGE_NOT_SPECIFIED][$delta];
$output = field_view_value('test_entity', $this->entity, $this->field_name, $item);
$this->drupalSetContent(drupal_render($output));
$this->assertText($setting . '|' . $value['value'], t('Value @delta was displayed with expected setting.', array('@delta' => $delta)));
}
// Check that explicit display settings are used.
$display = array(
'type' => 'field_test_multiple',
'settings' => array(
'test_formatter_setting_multiple' => $this->randomName(),
),
);
$setting = $display['settings']['test_formatter_setting_multiple'];
$array = array();
foreach ($this->values as $delta => $value) {
$item = $this->entity->{$this->field_name}[LANGUAGE_NOT_SPECIFIED][$delta];
$output = field_view_value('test_entity', $this->entity, $this->field_name, $item, $display);
$this->drupalSetContent(drupal_render($output));
$this->assertText($setting . '|0:' . $value['value'], t('Value @delta was displayed with expected setting.', array('@delta' => $delta)));
}
// Check that prepare_view steps are invoked.
$display = array(
'type' => 'field_test_with_prepare_view',
'settings' => array(
'test_formatter_setting_additional' => $this->randomName(),
),
);
$setting = $display['settings']['test_formatter_setting_additional'];
$array = array();
foreach ($this->values as $delta => $value) {
$item = $this->entity->{$this->field_name}[LANGUAGE_NOT_SPECIFIED][$delta];
$output = field_view_value('test_entity', $this->entity, $this->field_name, $item, $display);
$this->drupalSetContent(drupal_render($output));
$this->assertText($setting . '|' . $value['value'] . '|' . ($value['value'] + 1), t('Value @delta was displayed with expected setting.', array('@delta' => $delta)));
}
// View mode: check that display settings specified in the instance are
// used.
$setting = $this->instance['display']['teaser']['settings']['test_formatter_setting'];
foreach ($this->values as $delta => $value) {
$item = $this->entity->{$this->field_name}[LANGUAGE_NOT_SPECIFIED][$delta];
$output = field_view_value('test_entity', $this->entity, $this->field_name, $item, 'teaser');
$this->drupalSetContent(drupal_render($output));
$this->assertText($setting . '|' . $value['value'], t('Value @delta was displayed with expected setting.', array('@delta' => $delta)));
}
// Unknown view mode: check that display settings for 'default' view mode
// are used.
$setting = $this->instance['display']['default']['settings']['test_formatter_setting'];
foreach ($this->values as $delta => $value) {
$item = $this->entity->{$this->field_name}[LANGUAGE_NOT_SPECIFIED][$delta];
$output = field_view_value('test_entity', $this->entity, $this->field_name, $item, 'unknown_view_mode');
$this->drupalSetContent(drupal_render($output));
$this->assertText($setting . '|' . $value['value'], t('Value @delta was displayed with expected setting.', array('@delta' => $delta)));
}
}
}
<?php
/**
* @file
* Definition of Drupal\field\Tests\EntityPropertiesTest.
*/
namespace Drupal\field\Tests;
/**
* Tests entity properties.
*/
class EntityPropertiesTest extends FieldTestBase {
public static function getInfo() {
return array(
'name' => 'Entity properties',
'description' => 'Tests entity properties.',
'group' => 'Entity API',
);
}
function setUp() {
parent::setUp('field_test');
}
/**
* Tests label key and label callback of an entity.
*/
function testEntityLabel() {
$entity_types = array(
'test_entity_no_label',
'test_entity_label',
'test_entity_label_callback',
);
$entity = field_test_create_stub_entity();
foreach ($entity_types as $entity_type) {
$label = entity_label($entity_type, $entity);
switch ($entity_type) {
case 'test_entity_no_label':
$this->assertFalse($label, 'Entity with no label property or callback returned FALSE.');
break;
case 'test_entity_label':
$this->assertEqual($label, $entity->ftlabel, 'Entity with label key returned correct label.');
break;
case 'test_entity_label_callback':
$this->assertEqual($label, 'label callback ' . $entity->ftlabel, 'Entity with label callback returned correct label.');
break;
}
}
}
}
<?php
/**
* @file
* Definition of Drupal\field\Tests\FieldAttachTestBase.
*/
namespace Drupal\field\Tests;
class FieldAttachTestBase extends FieldTestBase {
function setUp() {
// Since this is a base class for many test cases, support the same
// flexibility that Drupal\simpletest\WebTestBase::setUp() has for the
// modules to be passed in as either an array or a variable number of string
// arguments.
$modules = func_get_args();
if (isset($modules[0]) && is_array($modules[0])) {
$modules = $modules[0];
}
if (!in_array('field_test', $modules)) {
$modules[] = 'field_test';
}
parent::setUp($modules);
$this->field_name = drupal_strtolower($this->randomName() . '_field_name');
$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);
}
}
This diff is collapsed.
<?php
/**
* @file
* Definition of Drupal\field\Tests\FieldTestBase.
*/
namespace Drupal\field\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Parent class for Field API tests.
*/
class FieldTestBase extends WebTestBase {
var $default_storage = 'field_sql_storage';
/**
* Set the default field storage backend for fields created during tests.
*/
function setUp() {
// Since this is a base class for many test cases, support the same
// flexibility that Drupal\simpletest\WebTestBase::setUp() has for the
// modules to be passed in as either an array or a variable number of string
// arguments.
$modules = func_get_args();
if (isset($modules[0]) && is_array($modules[0])) {
$modules = $modules[0];
}
parent::setUp($modules);
// Set default storage backend.
variable_set('field_storage_default', $this->default_storage);
}
/**
* 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 $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($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), t('Expected number of values were saved.'));
foreach ($expected_values as $key => $value) {
$this->assertEqual($values[$key][$column], $value, t('Value @value was saved correctly.', array('@value' => $value)));
}
}
}
This diff is collapsed.
This diff is collapsed.
......@@ -4,5 +4,4 @@ package = Core
version = VERSION
core = 8.x
dependencies[] = field
files[] = field_sql_storage.test
required = TRUE
<?php
/**
* @file
* Definition of Drupal\field_sql_storage\FieldSqlStorageTest.
*/
namespace Drupal\field_sql_storage\Tests;
use Drupal\Core\Database\Database;
use Drupal\field\FieldException;
use Drupal\simpletest\WebTestBase;
use Exception;
use PDO;
/**
* @file
* Tests for field_sql_storage.module.
* Tests field storage.
*
* Field_sql_storage.module implements the default back-end storage plugin
* for the Field Strage API.
*/
/**
* Tests field storage.
*/
class FieldSqlStorageTestCase extends WebTestBase {
class FieldSqlStorageTest extends WebTestBase {
public static function getInfo() {
return array(
'name' => 'Field SQL Storage tests',
......
......@@ -6,3 +6,4 @@ core = 8.x
dependencies[] = field
dependencies[] = options
files[] = tests/list.test
files[] = tests/list_dynamic.test
......@@ -7,11 +7,12 @@
use Drupal\field\FieldException;
use Drupal\field\FieldValidationException;
use Drupal\field\Tests\FieldTestBase;
/**
* Tests for the 'List' field types.
*/
class ListFieldTestCase extends FieldTestCase {
class ListFieldTestBase extends FieldTestBase {
public static function getInfo() {
return array(
'name' => 'List field',
......@@ -116,46 +117,6 @@ class ListFieldTestCase extends FieldTestCase {
}
}
/**
* Sets up a List field for testing allowed values functions.
*/
class ListDynamicValuesTestCase extends FieldTestCase {
function setUp() {
parent::setUp(array('list', 'field_test', 'list_test'));
$this->field_name = 'test_list';
$this->field = array(
'field_name' => $this->field_name,
'type' => 'list_text',
'cardinality' => 1,
'settings' => array(
'allowed_values_function' => 'list_test_dynamic_values_callback',
),
);
$this->field = field_create_field($this->field);
$this->instance = array(
'field_name' => $this->field_name,
'entity_type' => 'test_entity',
'bundle' => 'test_bundle',
'required' => TRUE,
'widget' => array(
'type' => 'options_select',
),
);
$this->instance = field_create_instance($this->instance);
$this->test = array(
'id' => mt_rand(1, 10),
// Make sure this does not equal the ID so that
// list_test_dynamic_values_callback() always returns 4 values.
'vid' => mt_rand(20, 30),
'bundle' => 'test_bundle',
'label' => $this->randomName(),
);
$this->entity = call_user_func_array('field_test_create_stub_entity', $this->test);
}
}
/**
* Tests the List field allowed values function.
*/
......@@ -202,7 +163,7 @@ class ListDynamicValuesValidationTestCase extends ListDynamicValuesTestCase {
/**
* List module UI tests.
*/
class ListFieldUITestCase extends FieldTestCase {
class ListFieldUITestCase extends FieldTestBase {
public static function getInfo() {
return array(
'name' => 'List field UI',
......
<?php
/**
* @file
* Tests for list.module.
*/
use Drupal\field\Tests\FieldTestBase;
/**
* Sets up a List field for testing allowed values functions.
*/
class ListDynamicValuesTestCase extends FieldTestBase {
function setUp() {
parent::setUp(array('list', 'field_test', 'list_test'));
$this->field_name = 'test_list';
$this->field = array(
'field_name' => $this->field_name,
'type' => 'list_text',
'cardinality' => 1,
'settings' => array(
'allowed_values_function' => 'list_test_dynamic_values_callback',
),
);
$this->field = field_create_field($this->field);
$this->instance = array(
'field_name' => $this->field_name,
'entity_type' => 'test_entity',
'bundle' => 'test_bundle',
'required' => TRUE,
'widget' => array(
'type' => 'options_select',
),
);
$this->instance = field_create_instance($this->instance);
$this->test = array(
'id' => mt_rand(1, 10),
// Make sure this does not equal the ID so that
// list_test_dynamic_values_callback() always returns 4 values.
'vid' => mt_rand(20, 30),
'bundle' => 'test_bundle',
'label' => $this->randomName(),
);
$this->entity = call_user_func_array('field_test_create_stub_entity', $this->test);
}
}
......@@ -2,15 +2,17 @@
/**
* @file
* Tests for number.module.
* Definition of Drupal\number\NumberFieldTest.
*/
namespace Drupal\number\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Tests for number field types.
*/
class NumberFieldTestCase extends WebTestBase {
class NumberFieldTest extends WebTestBase {
protected $field;
protected $instance;
protected $web_user;
......
......@@ -4,4 +4,3 @@ package = Core
version = VERSION
core = 8.x
dependencies[] = field
files[] = number.test
<?php
/**
* @file
* Definition of Drupal\options\OptionsWidgetsTest.
*/
namespace Drupal\options\Tests;
use ListDynamicValuesTestCase;
/**
* Test an options select on a list field with a dynamic allowed values function.
*/
class OptionsSelectDynamicValuesTest extends ListDynamicValuesTestCase {
public static function getInfo() {
return array(
'name' => 'Options select dynamic values',
'description' => 'Test an options select on a list field with a dynamic allowed values function.',
'group' => 'Field types',
);
}
/**
* Tests the 'options_select' widget (single select).
*/
function testSelectListDynamic() {
// Create an entity.
$this->entity->is_new = TRUE;
field_test_entity_save($this->entity);
// Create a web user.
$web_user = $this->drupalCreateUser(array('access field_test content', 'administer field_test content'));
$this->drupalLogin($web_user);
// Display form.
$this->drupalGet('test-entity/manage/' . $this->entity->ftid . '/edit');
$options = $this->xpath('//select[@id="edit-test-list-und"]/option');
$this->assertEqual(count($options), count($this->test) + 1);
foreach ($options as $option) {