Commit bf2c404e authored by alexpott's avatar alexpott

Issue #2004224 by Berdir, andypost: Convert tests using TestEntity to...

Issue #2004224 by Berdir, andypost: Convert tests using TestEntity to EntityTest (except Field API tests).
parent 23357e25
......@@ -21,7 +21,7 @@ class DatetimeFieldTest extends WebTestBase {
*
* @var array
*/
public static $modules = array('node', 'field_test', 'datetime', 'field_ui');
public static $modules = array('node', 'entity_test', 'datetime', 'field_ui');
/**
* A field to use in this test class.
......@@ -42,8 +42,8 @@ function setUp() {
parent::setUp();
$web_user = $this->drupalCreateUser(array(
'access field_test content',
'administer field_test content',
'view test entity',
'administer entity_test content',
'administer content types',
));
$this->drupalLogin($web_user);
......@@ -56,8 +56,8 @@ function setUp() {
));
$this->instance = field_create_instance(array(
'field_name' => $this->field['field_name'],
'entity_type' => 'test_entity',
'bundle' => 'test_bundle',
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
'settings' => array(
'default_value' => 'blank',
),
......@@ -85,7 +85,7 @@ function setUp() {
function testDateField() {
// Display creation form.
$this->drupalGet('test-entity/add/test_bundle');
$this->drupalGet('entity_test/add');
$langcode = Language::LANGCODE_NOT_SPECIFIED;
$this->assertFieldByName("{$this->field['field_name']}[$langcode][0][value][date]", '', 'Date element found.');
$this->assertNoFieldByName("{$this->field['field_name']}[$langcode][0][value][time]", '', 'Time element not found.');
......@@ -98,12 +98,14 @@ function testDateField() {
$time_format = config('system.date')->get('formats.html_time.pattern.' . $format_type);
$edit = array(
'user_id' => 1,
'name' => $this->randomName(),
"{$this->field['field_name']}[$langcode][0][value][date]" => $date->format($date_format),
);
$this->drupalPost(NULL, $edit, t('Save'));
preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match);
preg_match('|entity_test/manage/(\d+)/edit|', $this->url, $match);
$id = $match[1];
$this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)));
$this->assertText(t('entity_test @id has been created.', array('@id' => $id)));
$this->assertRaw($date->format($date_format));
$this->assertNoRaw($date->format($time_format));
......@@ -154,7 +156,7 @@ function testDatetimeField() {
field_update_field($this->field);
// Display creation form.
$this->drupalGet('test-entity/add/test_bundle');
$this->drupalGet('entity_test/add');
$langcode = Language::LANGCODE_NOT_SPECIFIED;
$this->assertFieldByName("{$this->field['field_name']}[$langcode][0][value][date]", '', 'Date element found.');
$this->assertFieldByName("{$this->field['field_name']}[$langcode][0][value][time]", '', 'Time element found.');
......@@ -167,13 +169,15 @@ function testDatetimeField() {
$time_format = config('system.date')->get('formats.html_time.pattern.' . $format_type);
$edit = array(
'user_id' => 1,
'name' => $this->randomName(),
"{$this->field['field_name']}[$langcode][0][value][date]" => $date->format($date_format),
"{$this->field['field_name']}[$langcode][0][value][time]" => $date->format($time_format),
);
$this->drupalPost(NULL, $edit, t('Save'));
preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match);
preg_match('|entity_test/manage/(\d+)/edit|', $this->url, $match);
$id = $match[1];
$this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)));
$this->assertText(t('entity_test @id has been created.', array('@id' => $id)));
$this->assertRaw($date->format($date_format));
$this->assertRaw($date->format($time_format));
......@@ -234,7 +238,7 @@ function testDatelistWidget() {
field_cache_clear();
// Display creation form.
$this->drupalGet('test-entity/add/test_bundle');
$this->drupalGet('entity_test/add');
$field_name = $this->field['field_name'];
$langcode = Language::LANGCODE_NOT_SPECIFIED;
......@@ -256,7 +260,10 @@ function testDatelistWidget() {
$date_value = array('year' => 2012, 'month' => 12, 'day' => 31, 'hour' => 5, 'minute' => 15);
$date = new DrupalDateTime($date_value);
$edit = array();
$edit = array(
'user_id' => 1,
'name' => $this->randomName(),
);
// Add the ampm indicator since we are testing 12 hour time.
$date_value['ampm'] = 'am';
foreach ($date_value as $part => $value) {
......@@ -264,9 +271,9 @@ function testDatelistWidget() {
}
$this->drupalPost(NULL, $edit, t('Save'));
preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match);
preg_match('|entity_test/manage/(\d+)/edit|', $this->url, $match);
$id = $match[1];
$this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)));
$this->assertText(t('entity_test @id has been created.', array('@id' => $id)));
$this->assertOptionSelected("edit-$field_name-$langcode-0-value-year", '2012', 'Correct year selected.');
$this->assertOptionSelected("edit-$field_name-$langcode-0-value-month", '12', 'Correct month selected.');
......@@ -293,7 +300,7 @@ function testDefaultValue() {
// Display creation form.
$date = new DrupalDateTime();
$date_format = 'Y-m-d';
$this->drupalGet('test-entity/add/test_bundle');
$this->drupalGet('entity_test/add');
$langcode = Language::LANGCODE_NOT_SPECIFIED;
// See if current date is set. We cannot test for the precise time because
......@@ -310,7 +317,7 @@ function testDefaultValue() {
// Display creation form.
$date = new DrupalDateTime();
$this->drupalGet('test-entity/add/test_bundle');
$this->drupalGet('entity_test/add');
// See that no date is set.
$this->assertFieldByName("{$this->field['field_name']}[$langcode][0][value][date]", '', 'Date element found.');
......@@ -327,7 +334,7 @@ function testInvalidField() {
field_update_field($this->field);
// Display creation form.
$this->drupalGet('test-entity/add/test_bundle');
$this->drupalGet('entity_test/add');
$langcode = Language::LANGCODE_NOT_SPECIFIED;
$this->assertFieldByName("{$this->field['field_name']}[$langcode][0][value][date]", '', 'Date element found.');
$this->assertFieldByName("{$this->field['field_name']}[$langcode][0][value][time]", '', 'Time element found.');
......@@ -403,23 +410,23 @@ function testInvalidField() {
}
/**
* Renders a test_entity and sets the output in the internal browser.
* Renders a entity_test and sets the output in the internal browser.
*
* @param int $id
* The test_entity ID to render.
* The entity_test ID to render.
* @param string $view_mode
* (optional) The view mode to use for rendering. Defaults to 'full'.
* @param bool $reset
* (optional) Whether to reset the test_entity controller cache. Defaults to
* (optional) Whether to reset the entity_test controller cache. Defaults to
* TRUE to simplify testing.
*/
protected function renderTestEntity($id, $view_mode = 'full', $reset = TRUE) {
if ($reset) {
entity_get_controller('test_entity')->resetCache(array($id));
entity_get_controller('entity_test')->resetCache(array($id));
}
$entity = field_test_entity_test_load($id);
$display = entity_get_display('test_entity', $entity->bundle(), 'full');
field_attach_prepare_view('test_entity', array($entity->id() => $entity), array($entity->bundle() => $display), $view_mode);
$entity = entity_load('entity_test', $id);
$display = entity_get_display('entity_test', $entity->bundle(), 'full');
field_attach_prepare_view('entity_test', array($entity->id() => $entity), array($entity->bundle() => $display), $view_mode);
$entity->content = field_attach_view($entity, $display, $view_mode);
$output = drupal_render($entity->content);
......
......@@ -20,7 +20,7 @@ class EmailFieldTest extends WebTestBase {
*
* @var array
*/
public static $modules = array('node', 'field_test', 'email', 'field_ui');
public static $modules = array('node', 'entity_test', 'email', 'field_ui');
public static function getInfo() {
return array(
......@@ -34,8 +34,8 @@ function setUp() {
parent::setUp();
$this->web_user = $this->drupalCreateUser(array(
'access field_test content',
'administer field_test content',
'view test entity',
'administer entity_test content',
'administer content types',
));
$this->drupalLogin($this->web_user);
......@@ -53,13 +53,13 @@ function testEmailField() {
field_create_field($this->field);
$this->instance = array(
'field_name' => $this->field['field_name'],
'entity_type' => 'test_entity',
'bundle' => 'test_bundle',
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
);
field_create_instance($this->instance);
// Create a form display for the default form mode.
entity_get_form_display('test_entity', 'test_bundle', 'default')
entity_get_form_display('entity_test', 'entity_test', 'default')
->setComponent($this->field['field_name'], array(
'type' => 'email_default',
'settings' => array(
......@@ -68,14 +68,14 @@ function testEmailField() {
))
->save();
// Create a display for the full view mode.
entity_get_display('test_entity', 'test_bundle', 'full')
entity_get_display('entity_test', 'entity_test', 'full')
->setComponent($this->field['field_name'], array(
'type' => 'email_mailto',
))
->save();
// Display creation form.
$this->drupalGet('test-entity/add/test_bundle');
$this->drupalGet('entity_test/add');
$langcode = Language::LANGCODE_NOT_SPECIFIED;
$this->assertFieldByName("{$this->field['field_name']}[$langcode][0][value]", '', 'Widget found.');
$this->assertRaw('placeholder="example@example.com"');
......@@ -83,16 +83,18 @@ function testEmailField() {
// Submit a valid e-mail address and ensure it is accepted.
$value = 'test@example.com';
$edit = array(
'user_id' => 1,
'name' => $this->randomName(),
"{$this->field['field_name']}[$langcode][0][value]" => $value,
);
$this->drupalPost(NULL, $edit, t('Save'));
preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match);
preg_match('|entity_test/manage/(\d+)/edit|', $this->url, $match);
$id = $match[1];
$this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)));
$this->assertText(t('entity_test @id has been created.', array('@id' => $id)));
$this->assertRaw($value);
// Verify that a mailto link is displayed.
$entity = field_test_entity_test_load($id);
$entity = entity_load('entity_test', $id);
$display = entity_get_display($entity->entityType(), $entity->bundle(), 'full');
$entity->content = field_attach_view($entity, $display);
$this->drupalSetContent(drupal_render($entity->content));
......
......@@ -269,6 +269,9 @@ function field_entity_create(EntityInterface $entity) {
* Defaults to the entity language.
*/
function field_populate_default_values(EntityInterface $entity, $langcode = NULL) {
// Ensure we are working with a BC mode entity.
$entity = $entity->getBCEntity();
$entity_type = $entity->entityType();
$langcode = $langcode ?: $entity->language()->langcode;
foreach (field_info_instances($entity_type, $entity->bundle()) as $field_name => $instance) {
......
......@@ -49,9 +49,13 @@ function _generateTestFieldValues($cardinality) {
* (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();
// Re-load the entity to make sure we have the latest changes.
entity_get_controller($entity->entityType())->resetCache(array($entity->id()));
$e = entity_load($entity->entityType(), $entity->id());
$field = $values = $e->getTranslation($langcode, FALSE)->$field_name;
// Filter out empty values so that they don't mess with the assertions.
$field->filterEmptyValues();
$values = $field->getValue();
$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)));
......
......@@ -7,6 +7,7 @@
namespace Drupal\field\Tests;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Language\Language;
class FormTest extends FieldTestBase {
......@@ -679,4 +680,21 @@ function testFieldFormHiddenWidget() {
$entity = field_test_entity_test_load($id);
$this->assertEqual($entity->{$this->field_name}[$langcode][0]['value'], $value, 'New revision has the expected value for the field with the Hidden widget');
}
/**
* {inheritdoc}
*/
function assertFieldValues(EntityInterface $entity, $field_name, $langcode, $expected_values, $column = 'value') {
// Override the base implementation with one that works with the old
// entity API.
// @todo: Remove this when replacing the remaining of test_entity with
// entity_test.
$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)));
}
}
}
......@@ -20,7 +20,7 @@ class LinkFieldTest extends WebTestBase {
*
* @var array
*/
public static $modules = array('field_test', 'link');
public static $modules = array('entity_test', 'link');
public static function getInfo() {
return array(
......@@ -34,8 +34,8 @@ function setUp() {
parent::setUp();
$this->web_user = $this->drupalCreateUser(array(
'access field_test content',
'administer field_test content',
'view test entity',
'administer entity_test content',
));
$this->drupalLogin($this->web_user);
}
......@@ -52,14 +52,14 @@ function testURLValidation() {
field_create_field($this->field);
$this->instance = array(
'field_name' => $this->field['field_name'],
'entity_type' => 'test_entity',
'bundle' => 'test_bundle',
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
'settings' => array(
'title' => DRUPAL_DISABLED,
),
);
field_create_instance($this->instance);
entity_get_form_display('test_entity', 'test_bundle', 'default')
entity_get_form_display('entity_test', 'entity_test', 'default')
->setComponent($this->field['field_name'], array(
'type' => 'link_default',
'settings' => array(
......@@ -67,7 +67,7 @@ function testURLValidation() {
),
))
->save();
entity_get_display('test_entity', 'test_bundle', 'full')
entity_get_display('entity_test', 'entity_test', 'full')
->setComponent($this->field['field_name'], array(
'type' => 'link',
))
......@@ -76,19 +76,21 @@ function testURLValidation() {
$langcode = Language::LANGCODE_NOT_SPECIFIED;
// Display creation form.
$this->drupalGet('test-entity/add/test_bundle');
$this->drupalGet('entity_test/add');
$this->assertFieldByName("{$this->field['field_name']}[$langcode][0][url]", '', 'Link URL field is displayed');
$this->assertRaw('placeholder="http://example.com"');
// Verify that a valid URL can be submitted.
$value = 'http://www.example.com/';
$edit = array(
'user_id' => 1,
'name' => $this->randomName(),
"{$this->field['field_name']}[$langcode][0][url]" => $value,
);
$this->drupalPost(NULL, $edit, t('Save'));
preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match);
preg_match('|entity_test/manage/(\d+)/edit|', $this->url, $match);
$id = $match[1];
$this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)));
$this->assertText(t('entity_test @id has been created.', array('@id' => $id)));
$this->assertRaw($value);
// Verify that invalid URLs cannot be submitted.
......@@ -100,9 +102,11 @@ function testURLValidation() {
// Missing host name
'http://',
);
$this->drupalGet('test-entity/add/test_bundle');
$this->drupalGet('entity_test/add');
foreach ($wrong_entries as $invalid_value) {
$edit = array(
'user_id' => 1,
'name' => $this->randomName(),
"{$this->field['field_name']}[$langcode][0][url]" => $invalid_value,
);
$this->drupalPost(NULL, $edit, t('Save'));
......@@ -122,15 +126,15 @@ function testLinkTitle() {
field_create_field($this->field);
$this->instance = array(
'field_name' => $this->field['field_name'],
'entity_type' => 'test_entity',
'bundle' => 'test_bundle',
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
'label' => 'Read more about this entity',
'settings' => array(
'title' => DRUPAL_OPTIONAL,
),
);
field_create_instance($this->instance);
entity_get_form_display('test_entity', 'test_bundle', 'default')
entity_get_form_display('entity_test', 'entity_test', 'default')
->setComponent($this->field['field_name'], array(
'type' => 'link_default',
'settings' => array(
......@@ -139,7 +143,7 @@ function testLinkTitle() {
),
))
->save();
entity_get_display('test_entity', 'test_bundle', 'full')
entity_get_display('entity_test', 'entity_test', 'full')
->setComponent($this->field['field_name'], array(
'type' => 'link',
'label' => 'hidden',
......@@ -155,7 +159,7 @@ function testLinkTitle() {
field_update_instance($this->instance);
// Display creation form.
$this->drupalGet('test-entity/add/test_bundle');
$this->drupalGet('entity_test/add');
// Assert label is shown.
$this->assertText('Read more about this entity');
$this->assertFieldByName("{$this->field['field_name']}[$langcode][0][url]", '', 'URL field found.');
......@@ -185,7 +189,7 @@ function testLinkTitle() {
$this->assertNoText(t('!name field is required.', array('!name' => t('Link text'))));
// Verify that a URL and link text meets requirements.
$this->drupalGet('test-entity/add/test_bundle');
$this->drupalGet('entity_test/add');
$edit = array(
"{$this->field['field_name']}[$langcode][0][url]" => 'http://www.example.com',
"{$this->field['field_name']}[$langcode][0][title]" => 'Example',
......@@ -199,13 +203,15 @@ function testLinkTitle() {
// Verify that a link without link text is rendered using the URL as text.
$value = 'http://www.example.com/';
$edit = array(
'user_id' => 1,
'name' => $this->randomName(),
"{$this->field['field_name']}[$langcode][0][url]" => $value,
"{$this->field['field_name']}[$langcode][0][title]" => '',
);
$this->drupalPost(NULL, $edit, t('Save'));
preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match);
preg_match('|entity_test/manage/(\d+)/edit|', $this->url, $match);
$id = $match[1];
$this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)));
$this->assertText(t('entity_test @id has been created.', array('@id' => $id)));
$this->renderTestEntity($id);
$expected_link = l($value, $value);
......@@ -214,10 +220,12 @@ function testLinkTitle() {
// Verify that a link with text is rendered using the link text.
$title = $this->randomName();
$edit = array(
'user_id' => 1,
'name' => $this->randomName(),
"{$this->field['field_name']}[$langcode][0][title]" => $title,
);
$this->drupalPost("test-entity/manage/$id/edit", $edit, t('Save'));
$this->assertRaw(t('test_entity @id has been updated.', array('@id' => $id)));
$this->drupalPost("entity_test/manage/$id/edit", $edit, t('Save'));
$this->assertText(t('entity_test @id has been updated.', array('@id' => $id)));
$this->renderTestEntity($id);
$expected_link = l($title, $value);
......@@ -237,9 +245,9 @@ function testLinkFormatter() {
field_create_field($this->field);
$this->instance = array(
'field_name' => $this->field['field_name'],
'entity_type' => 'test_entity',
'entity_type' => 'entity_test',
'label' => 'Read more about this entity',
'bundle' => 'test_bundle',
'bundle' => 'entity_test',
'settings' => array(
'title' => DRUPAL_OPTIONAL,
),
......@@ -249,12 +257,12 @@ function testLinkFormatter() {
'label' => 'hidden',
);
field_create_instance($this->instance);
entity_get_form_display('test_entity', 'test_bundle', 'default')
entity_get_form_display('entity_test', 'entity_test', 'default')
->setComponent($this->field['field_name'], array(
'type' => 'link_default',
))
->save();
entity_get_display('test_entity', 'test_bundle', 'full')
entity_get_display('entity_test', 'entity_test', 'full')
->setComponent($this->field['field_name'], $display_options)
->save();
......@@ -265,13 +273,15 @@ function testLinkFormatter() {
// - The second field item uses a URL and link text.
// For consistency in assertion code below, the URL is assigned to the title
// variable for the first field.
$this->drupalGet('test-entity/add/test_bundle');
$this->drupalGet('entity_test/add');
$url1 = 'http://www.example.com/content/articles/archive?author=John&year=2012#com';
$url2 = 'http://www.example.org/content/articles/archive?author=John&year=2012#org';
$title1 = $url1;
// Intentionally contains an ampersand that needs sanitization on output.
$title2 = 'A very long & strange example title that could break the nice layout of the site';
$edit = array(
'user_id' => 1,
'name' => $this->randomName(),
"{$this->field['field_name']}[$langcode][0][url]" => $url1,
// Note that $title1 is not submitted.
"{$this->field['field_name']}[$langcode][0][title]" => '',
......@@ -281,9 +291,9 @@ function testLinkFormatter() {
// Assert label is shown.
$this->assertText('Read more about this entity');
$this->drupalPost(NULL, $edit, t('Save'));
preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match);
preg_match('|entity_test/manage/(\d+)/edit|', $this->url, $match);
$id = $match[1];
$this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)));
$this->assertText(t('entity_test @id has been created.', array('@id' => $id)));
// Verify that the link is output according to the formatter settings.
// Not using generatePermutations(), since that leads to 32 cases, which
......@@ -310,7 +320,7 @@ function testLinkFormatter() {
else {
$display_options['settings'] = $new_value;
}
entity_get_display('test_entity', 'test_bundle', 'full')
entity_get_display('entity_test', 'entity_test', 'full')
->setComponent($this->field['field_name'], $display_options)
->save();
......@@ -378,8 +388,8 @@ function testLinkSeparateFormatter() {
field_create_field($this->field);
$this->instance = array(
'field_name' => $this->field['field_name'],
'entity_type' => 'test_entity',
'bundle' => 'test_bundle',
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
'settings' => array(
'title' => DRUPAL_OPTIONAL,
),
......@@ -389,12 +399,12 @@ function testLinkSeparateFormatter() {
'label' => 'hidden',
);
field_create_instance($this->instance);
entity_get_form_display('test_entity', 'test_bundle', 'default')
entity_get_form_display('entity_test', 'entity_test', 'default')
->setComponent($this->field['field_name'], array(
'type' => 'link_default',
))
->save();
entity_get_display('test_entity', 'test_bundle', 'full')
entity_get_display('entity_test', 'entity_test', 'full')
->setComponent($this->field['field_name'], $display_options)
->save();
......@@ -405,20 +415,22 @@ function testLinkSeparateFormatter() {
// - The second field item uses a URL and link text.
// For consistency in assertion code below, the URL is assigned to the title
// variable for the first field.
$this->drupalGet('test-entity/add/test_bundle');
$this->drupalGet('entity_test/add');
$url1 = 'http://www.example.com/content/articles/archive?author=John&year=2012#com';
$url2 = 'http://www.example.org/content/articles/archive?author=John&year=2012#org';
// Intentionally contains an ampersand that needs sanitization on output.
$title2 = 'A very long & strange example title that could break the nice layout of the site';
$edit = array(
'user_id' => 1,
'name' => $this->randomName(),
"{$this->field['field_name']}[$langcode][0][url]" => $url1,
"{$this->field['field_name']}[$langcode][1][url]" => $url2,
"{$this->field['field_name']}[$langcode][1][title]" => $title2,
);
$this->drupalPost(NULL, $edit, t('Save'));
preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match);
preg_match('|entity_test/manage/(\d+)/edit|', $this->url, $match);
$id = $match[1];
$this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)));
$this->assertText(t('entity_test @id has been created.', array('@id' => $id)));
// Verify that the link is output according to the formatter settings.
$options = array(
......@@ -430,7 +442,7 @@ function testLinkSeparateFormatter() {
foreach ($values as $new_value) {
// Update the field formatter settings.