Issue #2870444 by Lendude, michielnugter, jonathan1055, borisson_: Convert web tests to browser tests for field module

8.7.x
Alex Pott 2018-08-06 12:32:52 +01:00
parent 533dfa6479
commit 1950bd5f06
No known key found for this signature in database
GPG Key ID: 31905460D4A69276
11 changed files with 130 additions and 35 deletions

View File

@ -2,6 +2,8 @@
namespace Drupal\field\Tests\Views;
@trigger_error(__NAMESPACE__ . '\FieldTestBase is deprecated in Drupal 8.6.0 and will be removed before Drupal 9.0.0. Instead, use \Drupal\Tests\field\Functional\Views\FieldTestBase. See https://www.drupal.org/node/2971931.', E_USER_DEPRECATED);
use Drupal\field\Entity\FieldConfig;
use Drupal\node\Entity\NodeType;
use Drupal\views\Tests\ViewTestBase;
@ -17,6 +19,11 @@ use Drupal\field\Entity\FieldStorageConfig;
* - Use basic fields and make sure that the full wanted object is built.
* - Use relationships between different entity types, for example node and
* the node author(user).
*
* @deprecated in Drupal 8.6.0. Will be removed before Drupal 9.0.0. Use
* \Drupal\Tests\field\Functional\Views\FieldTestBase instead.
*
* @see https://www.drupal.org/node/2989020
*/
abstract class FieldTestBase extends ViewTestBase {

View File

@ -1,9 +1,9 @@
<?php
namespace Drupal\field\Tests;
namespace Drupal\field_test;
/**
* Helper class for \Drupal\field\Tests\FieldDefaultValueCallbackTest.
* Helper class for \Drupal\Tests\field\Functional\FieldDefaultValueCallbackTest.
*/
class FieldDefaultValueCallbackProvider {

View File

@ -1,18 +1,18 @@
<?php
namespace Drupal\field\Tests\Email;
namespace Drupal\Tests\field\Functional\Email;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\simpletest\WebTestBase;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\BrowserTestBase;
/**
* Tests email field functionality.
*
* @group field
*/
class EmailFieldTest extends WebTestBase {
class EmailFieldTest extends BrowserTestBase {
/**
* Modules to enable.
@ -90,7 +90,7 @@ class EmailFieldTest extends WebTestBase {
"{$field_name}[0][value]" => $value,
];
$this->drupalPostForm(NULL, $edit, t('Save'));
preg_match('|entity_test/manage/(\d+)|', $this->url, $match);
preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match);
$id = $match[1];
$this->assertText(t('entity_test @id has been created.', ['@id' => $id]));
$this->assertRaw($value);
@ -99,8 +99,8 @@ class EmailFieldTest extends WebTestBase {
$entity = EntityTest::load($id);
$display = entity_get_display($entity->getEntityTypeId(), $entity->bundle(), 'full');
$content = $display->build($entity);
$this->setRawContent(\Drupal::service('renderer')->renderRoot($content));
$this->assertLinkByHref('mailto:test@example.com');
$rendered_content = (string) \Drupal::service('renderer')->renderRoot($content);
$this->assertContains('href="mailto:test@example.com"', $rendered_content);
}
}

View File

@ -1,18 +1,21 @@
<?php
namespace Drupal\field\Tests\EntityReference;
namespace Drupal\Tests\field\Functional\EntityReference;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\simpletest\WebTestBase;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\TestFileCreationTrait;
/**
* Tests an autocomplete widget with file upload.
*
* @group entity_reference
*/
class EntityReferenceFileUploadTest extends WebTestBase {
class EntityReferenceFileUploadTest extends BrowserTestBase {
use TestFileCreationTrait;
public static $modules = ['entity_reference', 'node', 'file'];
@ -113,7 +116,7 @@ class EntityReferenceFileUploadTest extends WebTestBase {
$user1 = $this->drupalCreateUser(['access content', "create $this->referencingType content"]);
$this->drupalLogin($user1);
$test_file = current($this->drupalGetTestFiles('text'));
$test_file = current($this->getTestFiles('text'));
$edit['files[file_field_0]'] = \Drupal::service('file_system')->realpath($test_file->uri);
$this->drupalPostForm('node/add/' . $this->referencingType, $edit, 'Upload');
$this->assertResponse(200);

View File

@ -1,17 +1,17 @@
<?php
namespace Drupal\field\Tests;
namespace Drupal\Tests\field\Functional;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\simpletest\WebTestBase;
use Drupal\Tests\BrowserTestBase;
/**
* Tests the default value callback.
*
* @group field
*/
class FieldDefaultValueCallbackTest extends WebTestBase {
class FieldDefaultValueCallbackTest extends BrowserTestBase {
/**
* Modules to enable.
@ -81,7 +81,7 @@ class FieldDefaultValueCallbackTest extends WebTestBase {
// Set a default value callback instead, and the default field form should
// not be visible.
$field_config->setDefaultValueCallback('\Drupal\field\Tests\FieldDefaultValueCallbackProvider::calculateDefaultValue')->save();
$field_config->setDefaultValueCallback('\Drupal\field_test\FieldDefaultValueCallbackProvider::calculateDefaultValue')->save();
$this->drupalGet('/admin/structure/types/manage/article/fields/node.article.field_test');
$this->assertNoFieldByName('default_value_input[field_test][0][value]', 'Calculated default value', 'The default field form is not visible when a callback is defined.');
}

View File

@ -1,6 +1,6 @@
<?php
namespace Drupal\field\Tests;
namespace Drupal\Tests\field\Functional;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;

View File

@ -1,6 +1,6 @@
<?php
namespace Drupal\field\Tests;
namespace Drupal\Tests\field\Functional;
use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
@ -131,7 +131,7 @@ class FormTest extends FieldTestBase {
"{$field_name}[0][value]" => $value,
];
$this->drupalPostForm(NULL, $edit, t('Save'));
preg_match('|entity_test/manage/(\d+)|', $this->url, $match);
preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match);
$id = $match[1];
$this->assertText(t('entity_test @id has been created.', ['@id' => $id]), 'Entity was created');
$entity = EntityTest::load($id);
@ -190,7 +190,7 @@ class FormTest extends FieldTestBase {
"{$field_name}[0][value]" => '',
];
$this->drupalPostForm(NULL, $edit, t('Save'));
preg_match('|entity_test/manage/(\d+)|', $this->url, $match);
preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match);
$id = $match[1];
$this->assertText(t('entity_test @id has been created.', ['@id' => $id]), 'Entity was created.');
$entity = EntityTest::load($id);
@ -219,7 +219,7 @@ class FormTest extends FieldTestBase {
"{$field_name}[0][value]" => $value,
];
$this->drupalPostForm(NULL, $edit, t('Save'));
preg_match('|entity_test/manage/(\d+)|', $this->url, $match);
preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match);
$id = $match[1];
$this->assertText(t('entity_test @id has been created.', ['@id' => $id]), 'Entity was created');
$entity = EntityTest::load($id);
@ -300,7 +300,7 @@ class FormTest extends FieldTestBase {
// Submit the form and create the entity.
$this->drupalPostForm(NULL, $edit, t('Save'));
preg_match('|entity_test/manage/(\d+)|', $this->url, $match);
preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match);
$id = $match[1];
$this->assertText(t('entity_test @id has been created.', ['@id' => $id]), 'Entity was created');
$entity = EntityTest::load($id);
@ -418,7 +418,7 @@ class FormTest extends FieldTestBase {
$field_name => '1, 2, 3',
];
$this->drupalPostForm(NULL, $edit, t('Save'));
preg_match('|entity_test/manage/(\d+)|', $this->url, $match);
preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match);
$id = $match[1];
// Check that the values were saved.
@ -498,7 +498,7 @@ class FormTest extends FieldTestBase {
"{$field_name}[0][value]" => 1,
];
$this->drupalPostForm(NULL, $edit, t('Save'));
preg_match("|$entity_type/manage/(\d+)|", $this->url, $match);
preg_match("|$entity_type/manage/(\d+)|", $this->getUrl(), $match);
$id = $match[1];
// Check that the default value was saved.
@ -554,7 +554,7 @@ class FormTest extends FieldTestBase {
// the field that uses the hidden widget.
$this->assertNoField("{$field_name}[0][value]", 'The field does not appear in the form');
$this->drupalPostForm(NULL, [], t('Save'));
preg_match('|' . $entity_type . '/manage/(\d+)|', $this->url, $match);
preg_match('|' . $entity_type . '/manage/(\d+)|', $this->getUrl(), $match);
$id = $match[1];
$this->assertText(t('entity_test_rev @id has been created.', ['@id' => $id]), 'Entity was created');
$storage = $this->container->get('entity_type.manager')

View File

@ -0,0 +1,82 @@
<?php
namespace Drupal\Tests\field\Functional\Views;
use Drupal\field\Entity\FieldConfig;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\views\Functional\ViewTestBase;
use Drupal\views\Tests\ViewTestData;
use Drupal\field\Entity\FieldStorageConfig;
/**
* Provides some helper methods for testing fieldapi integration into views.
*
* @todo Test on a generic entity not on a node. What has to be tested:
* - Make sure that every wanted field is added to the according entity type.
* - Make sure the joins are done correctly.
* - Use basic fields and make sure that the full wanted object is built.
* - Use relationships between different entity types, for example node and
* the node author(user).
*/
abstract class FieldTestBase extends ViewTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = ['node', 'field_test_views'];
/**
* Stores the field definitions used by the test.
*
* @var array
*/
public $fieldStorages;
/**
* Stores the fields of the field storage. They have the same keys as the
* field storages.
*
* @var array
*/
public $fields;
protected function setUp($import_test_views = TRUE) {
parent::setUp($import_test_views);
// Ensure the page node type exists.
NodeType::create([
'type' => 'page',
'name' => 'page',
])->save();
ViewTestData::createTestViews(get_class($this), ['field_test_views']);
}
public function setUpFieldStorages($amount = 3, $type = 'string') {
// Create three fields.
$field_names = [];
for ($i = 0; $i < $amount; $i++) {
$field_names[$i] = 'field_name_' . $i;
$this->fieldStorages[$i] = FieldStorageConfig::create([
'field_name' => $field_names[$i],
'entity_type' => 'node',
'type' => $type,
]);
$this->fieldStorages[$i]->save();
}
return $field_names;
}
public function setUpFields($bundle = 'page') {
foreach ($this->fieldStorages as $key => $field_storage) {
$this->fields[$key] = FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => $bundle,
]);
$this->fields[$key]->save();
}
}
}

View File

@ -1,6 +1,6 @@
<?php
namespace Drupal\field\Tests\Views;
namespace Drupal\Tests\field\Functional\Views;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
@ -58,7 +58,7 @@ class FieldUITest extends FieldTestBase {
// Tests the available formatter options.
$result = $this->xpath('//select[@id=:id]/option', [':id' => 'edit-options-type']);
$options = array_map(function ($item) {
return (string) $item->attributes()->value[0];
return $item->getAttribute('value');
}, $result);
// @todo Replace this sort by assertArray once it's in.
sort($options, SORT_STRING);
@ -112,7 +112,7 @@ class FieldUITest extends FieldTestBase {
// Tests the available formatter options.
$result = $this->xpath('//select[@id=:id]/option', [':id' => 'edit-options-click-sort-column']);
$options = array_map(function ($item) {
return (string) $item->attributes()->value[0];
return (string) $item->getAttribute('value');
}, $result);
sort($options, SORT_STRING);
@ -143,15 +143,15 @@ class FieldUITest extends FieldTestBase {
// Verify that using a boolean field as a filter also results in using the
// boolean plugin.
$option = $this->xpath('//label[@for="edit-options-value-1"]');
$this->assertEqual(t('True'), (string) $option[0]);
$this->assertEqual(t('True'), $option[0]->getText());
$option = $this->xpath('//label[@for="edit-options-value-0"]');
$this->assertEqual(t('False'), (string) $option[0]);
$this->assertEqual(t('False'), $option[0]->getText());
// Expose the filter and see if the 'Any' option is added and if we can save
// it.
$this->drupalPostForm(NULL, [], 'Expose filter');
$option = $this->xpath('//label[@for="edit-options-value-all"]');
$this->assertEqual(t('- Any -'), (string) $option[0]);
$this->assertEqual(t('- Any -'), $option[0]->getText());
$this->drupalPostForm(NULL, ['options[value]' => 'All', 'options[expose][required]' => FALSE], 'Apply');
$this->drupalPostForm(NULL, [], 'Save');
$this->drupalGet('/admin/structure/views/nojs/handler/test_view_fieldapi/default/filter/field_boolean_value');

View File

@ -1,6 +1,6 @@
<?php
namespace Drupal\field\Tests\Views;
namespace Drupal\Tests\field\Functional\Views;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\views\ViewExecutable;

View File

@ -1,17 +1,20 @@
<?php
namespace Drupal\field\Tests;
namespace Drupal\Tests\field\Functional;
use Drupal\field\Entity\FieldConfig;
use Drupal\simpletest\WebTestBase;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\Traits\Core\CronRunTrait;
/**
* Tests the behavior of a field module after being disabled and re-enabled.
*
* @group field
*/
class reEnableModuleFieldTest extends WebTestBase {
class reEnableModuleFieldTest extends BrowserTestBase {
use CronRunTrait;
/**
* Modules to enable.