Commit 3a200fd1 authored by larowlan's avatar larowlan

Issue #3041696 by claudiu.cristea, Lendude: Convert FieldApiDataTest into a kernel test

parent 9bd884d5
<?php
namespace Drupal\Tests\views\Functional;
namespace Drupal\Tests\views\Kernel;
use Drupal\Component\Render\MarkupInterface;
use Drupal\field\Entity\FieldConfig;
......@@ -9,7 +9,6 @@
use Drupal\language\Entity\ContentLanguageSettings;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\field\Functional\Views\FieldTestBase;
use Drupal\views\Views;
/**
......@@ -17,12 +16,18 @@
*
* @group views
*/
class FieldApiDataTest extends FieldTestBase {
class FieldApiDataTest extends ViewsKernelTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['language'];
public static $modules = [
'field',
'filter',
'language',
'node',
'user',
];
/**
* {@inheritdoc}
......@@ -30,44 +35,57 @@ class FieldApiDataTest extends FieldTestBase {
public static $testViews = ['test_field_config_translation_filter'];
/**
* The nodes used by the translation filter tests.
*
* @var \Drupal\node\NodeInterface[]
* {@inheritdoc}
*/
protected $translationNodes;
protected function setUp($import_test_views = TRUE) {
parent::setUp($import_test_views);
$this->installEntitySchema('user');
$this->installEntitySchema('node');
$this->installSchema('node', ['node_access']);
}
$field_names = $this->setUpFieldStorages(4);
$this->fieldStorages['string_long'] = FieldStorageConfig::create([
'field_name' => 'string_long',
/**
* Unit testing the views data structure.
*
* We check data structure for both node and node revision tables.
*/
public function testViewsData() {
$field_storage_string = FieldStorageConfig::create([
'field_name' => 'field_string',
'entity_type' => 'node',
'type' => 'string',
]);
$field_storage_string->save();
$field_storage_string_long = FieldStorageConfig::create([
'field_name' => 'field_string_long',
'entity_type' => 'node',
'type' => 'string_long',
]);
$this->fieldStorages['string_long']->save();
$field_storage_string_long->save();
NodeType::create(['type' => 'page'])->save();
NodeType::create(['type' => 'article'])->save();
// Attach the field to nodes only.
$field = [
'field_name' => $field_names[0],
// Attach the field to nodes.
FieldConfig::create([
'field_name' => 'field_string',
'entity_type' => 'node',
'bundle' => 'page',
'label' => 'GiraffeA" label',
];
FieldConfig::create($field)->save();
])->save();
// Attach the string_long field to the page node type.
FieldConfig::create([
'field_name' => 'string_long',
'field_name' => 'field_string_long',
'entity_type' => 'node',
'bundle' => 'page',
'label' => 'string_long label',
])->save();
// Attach the same field to a different bundle with a different label.
$this->drupalCreateContentType(['type' => 'article']);
FieldConfig::create([
'field_name' => $field_names[0],
'field_name' => 'field_string',
'entity_type' => 'node',
'bundle' => 'article',
'label' => 'GiraffeB" label',
......@@ -76,132 +94,26 @@ protected function setUp($import_test_views = TRUE) {
// Now create some example nodes/users for the view result.
for ($i = 0; $i < 5; $i++) {
$edit = [
$field_names[0] => [(['value' => $this->randomMachineName()])],
'field_string' => [(['value' => $this->randomMachineName()])],
];
$nodes[] = $this->drupalCreateNode($edit);
$nodes[] = Node::create(['type' => 'page'] + $edit);
}
$bundles = [];
$bundles[] = $bundle = NodeType::create(['type' => 'bundle1']);
$bundle->save();
$bundles[] = $bundle = NodeType::create(['type' => 'bundle2']);
$bundle->save();
// Make the first field translatable on all bundles.
$field = FieldConfig::create([
'field_name' => $field_names[1],
'entity_type' => 'node',
'bundle' => $bundles[0]->id(),
'translatable' => TRUE,
]);
$field->save();
$field = FieldConfig::create([
'field_name' => $field_names[1],
'entity_type' => 'node',
'bundle' => $bundles[1]->id(),
'translatable' => TRUE,
]);
$field->save();
// Make the second field not translatable on any bundle.
$field = FieldConfig::create([
'field_name' => $field_names[2],
'entity_type' => 'node',
'bundle' => $bundles[0]->id(),
'translatable' => FALSE,
]);
$field->save();
$field = FieldConfig::create([
'field_name' => $field_names[2],
'entity_type' => 'node',
'bundle' => $bundles[1]->id(),
'translatable' => FALSE,
]);
$field->save();
// Make the last field translatable on some bundles.
$field = FieldConfig::create([
'field_name' => $field_names[3],
'entity_type' => 'node',
'bundle' => $bundles[0]->id(),
'translatable' => TRUE,
]);
$field->save();
$field = FieldConfig::create([
'field_name' => $field_names[3],
'entity_type' => 'node',
'bundle' => $bundles[1]->id(),
'translatable' => FALSE,
]);
$field->save();
// Create some example content.
ConfigurableLanguage::create([
'id' => 'es',
])->save();
ConfigurableLanguage::create([
'id' => 'fr',
])->save();
$config = ContentLanguageSettings::loadByEntityTypeBundle('node', $bundles[0]->id());
$config->setDefaultLangcode('es')
->setLanguageAlterable(TRUE)
->save();
$config = ContentLanguageSettings::loadByEntityTypeBundle('node', $bundles[1]->id());
$config->setDefaultLangcode('es')
->setLanguageAlterable(TRUE)
->save();
$node = Node::create([
'title' => 'Test title ' . $bundles[0]->id(),
'type' => $bundles[0]->id(),
'langcode' => 'es',
$field_names[1] => 'field name 1: es',
$field_names[2] => 'field name 2: es',
$field_names[3] => 'field name 3: es',
]);
$node->save();
$this->translationNodes[] = $node;
$translation = $node->addTranslation('fr');
$translation->{$field_names[1]}->value = 'field name 1: fr';
$translation->{$field_names[3]}->value = 'field name 3: fr';
$translation->title->value = $node->title->value;
$translation->save();
$node = Node::create([
'title' => 'Test title ' . $bundles[1]->id(),
'type' => $bundles[1]->id(),
'langcode' => 'es',
$field_names[1] => 'field name 1: es',
$field_names[2] => 'field name 2: es',
$field_names[3] => 'field name 3: es',
]);
$node->save();
$this->translationNodes[] = $node;
$translation = $node->addTranslation('fr');
$translation->{$field_names[1]}->value = 'field name 1: fr';
$translation->title->value = $node->title->value;
$translation->save();
}
/** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
$table_mapping = $this->container->get('entity_type.manager')
->getStorage('node')
->getTableMapping();
/**
* Unit testing the views data structure.
*
* We check data structure for both node and node revision tables.
*/
public function testViewsData() {
$table_mapping = \Drupal::entityManager()->getStorage('node')->getTableMapping();
$field_storage = $this->fieldStorages[0];
$current_table = $table_mapping->getDedicatedDataTableName($field_storage);
$revision_table = $table_mapping->getDedicatedRevisionTableName($field_storage);
$current_table = $table_mapping->getDedicatedDataTableName($field_storage_string);
$revision_table = $table_mapping->getDedicatedRevisionTableName($field_storage_string);
$data = $this->getViewsData();
$this->assertTrue(isset($data[$current_table]));
$this->assertTrue(isset($data[$revision_table]));
$this->assertArrayHasKey($current_table, $data);
$this->assertArrayHasKey($revision_table, $data);
// The node field should join against node_field_data.
$this->assertTrue(isset($data[$current_table]['table']['join']['node_field_data']));
$this->assertTrue(isset($data[$revision_table]['table']['join']['node_field_revision']));
$this->assertArrayHasKey('node_field_data', $data[$current_table]['table']['join']);
$this->assertArrayHasKey('node_field_revision', $data[$revision_table]['table']['join']);
$expected_join = [
'table' => $current_table,
......@@ -212,7 +124,7 @@ public function testViewsData() {
['left_field' => 'langcode', 'field' => 'langcode'],
],
];
$this->assertEqual($expected_join, $data[$current_table]['table']['join']['node_field_data']);
$this->assertSame($expected_join, $data[$current_table]['table']['join']['node_field_data']);
$expected_join = [
'table' => $revision_table,
'left_field' => 'vid',
......@@ -222,32 +134,31 @@ public function testViewsData() {
['left_field' => 'langcode', 'field' => 'langcode'],
],
];
$this->assertEqual($expected_join, $data[$revision_table]['table']['join']['node_field_revision']);
$this->assertSame($expected_join, $data[$revision_table]['table']['join']['node_field_revision']);
// Test click sortable for string field.
$this->assertTrue($data[$current_table][$field_storage->getName()]['field']['click sortable'], 'String field is click sortable.');
$this->assertTrue($data[$current_table][$field_storage_string->getName()]['field']['click sortable']);
// Click sort should only be on the primary field.
$this->assertTrue(empty($data[$revision_table][$field_storage->getName()]['field']['click sortable']), 'Non-primary fields are not click sortable');
$this->assertTrue(empty($data[$revision_table][$field_storage_string->getName()]['field']['click sortable']));
// Test click sortable for long text field.
$data_long = $this->getViewsData('string_long');
$field_storage_long = $this->fieldStorages['string_long'];
$current_table_long = $table_mapping->getDedicatedDataTableName($field_storage_long);
$this->assertTrue($data_long[$current_table_long][$field_storage_long->getName()]['field']['click sortable'], 'Long text field is click sortable.');
$data_long = $this->getViewsData('field_string_long');
$current_table_long = $table_mapping->getDedicatedDataTableName($field_storage_string_long);
$this->assertTrue($data_long[$current_table_long][$field_storage_string_long->getName()]['field']['click sortable']);
$this->assertTrue($data[$current_table][$field_storage->getName()]['help'] instanceof MarkupInterface);
$this->assertEqual($data[$current_table][$field_storage->getName()]['help'], 'Appears in: page, article. Also known as: Content: GiraffeB&quot; label');
$this->assertInstanceOf(MarkupInterface::class, $data[$current_table][$field_storage_string->getName()]['help']);
$this->assertEquals('Appears in: page, article. Also known as: Content: GiraffeB&quot; label', $data[$current_table][$field_storage_string->getName()]['help']);
$this->assertTrue($data[$current_table][$field_storage->getName() . '_value']['help'] instanceof MarkupInterface);
$this->assertEqual($data[$current_table][$field_storage->getName() . '_value']['help'], 'Appears in: page, article. Also known as: Content: GiraffeA&quot; label (field_name_0)');
$this->assertInstanceOf(MarkupInterface::class, $data[$current_table][$field_storage_string->getName() . '_value']['help']);
$this->assertEquals('Appears in: page, article. Also known as: Content: GiraffeA&quot; label (field_string)', $data[$current_table][$field_storage_string->getName() . '_value']['help']);
// Since each label is only used once, views_entity_field_label() will
// return a label using alphabetical sorting.
$this->assertEqual('GiraffeA&quot; label (field_name_0)', $data[$current_table][$field_storage->getName() . '_value']['title']);
$this->assertEquals('GiraffeA&quot; label (field_string)', $data[$current_table][$field_storage_string->getName() . '_value']['title']);
// Attach the same field to a different bundle with a different label.
$this->drupalCreateContentType(['type' => 'news']);
NodeType::create(['type' => 'news'])->save();
FieldConfig::create([
'field_name' => $this->fieldStorages[0]->getName(),
'field_name' => $field_storage_string->getName(),
'entity_type' => 'node',
'bundle' => 'news',
'label' => 'GiraffeB" label',
......@@ -257,26 +168,30 @@ public function testViewsData() {
// Now the 'GiraffeB&quot; label' is used twice and therefore will be
// selected by views_entity_field_label().
$this->assertEqual('GiraffeB&quot; label (field_name_0)', $data[$current_table][$field_storage->getName() . '_value']['title']);
$this->assertTrue($data[$current_table][$field_storage->getName()]['help'] instanceof MarkupInterface);
$this->assertEqual($data[$current_table][$field_storage->getName()]['help'], 'Appears in: page, article, news. Also known as: Content: GiraffeA&quot; label');
$this->assertEquals('GiraffeB&quot; label (field_string)', $data[$current_table][$field_storage_string->getName() . '_value']['title']);
$this->assertInstanceOf(MarkupInterface::class, $data[$current_table][$field_storage_string->getName()]['help']);
$this->assertEquals('Appears in: page, article, news. Also known as: Content: GiraffeA&quot; label', $data[$current_table][$field_storage_string->getName()]['help']);
}
/**
* Gets the views data for the field created in setUp().
*
* @param string $field_storage_key
* (optional) The optional field name.
*
* @return array
* Views data.
*/
protected function getViewsData($field_storage_key = 0) {
protected function getViewsData($field_storage_key = 'field_string') {
$views_data = $this->container->get('views.views_data');
$data = [];
// Check the table and the joins of the first field.
// Attached to node only.
// Check the table and the joins of the first field. Attached to node only.
/** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
$table_mapping = \Drupal::entityManager()->getStorage('node')->getTableMapping();
$current_table = $table_mapping->getDedicatedDataTableName($this->fieldStorages[$field_storage_key]);
$revision_table = $table_mapping->getDedicatedRevisionTableName($this->fieldStorages[$field_storage_key]);
$table_mapping = $this->container->get('entity_type.manager')->getStorage('node')->getTableMapping();
$field_storage = FieldStorageConfig::loadByName('node', $field_storage_key);
$current_table = $table_mapping->getDedicatedDataTableName($field_storage);
$revision_table = $table_mapping->getDedicatedRevisionTableName($field_storage);
$data[$current_table] = $views_data->get($current_table);
$data[$revision_table] = $views_data->get($revision_table);
return $data;
......@@ -286,6 +201,77 @@ protected function getViewsData($field_storage_key = 0) {
* Tests filtering entries with different translatability.
*/
public function testEntityFieldFilter() {
NodeType::create(['type' => 'bundle1'])->save();
NodeType::create(['type' => 'bundle2'])->save();
// Create some example content.
ConfigurableLanguage::create(['id' => 'es'])->save();
ConfigurableLanguage::create(['id' => 'fr'])->save();
ContentLanguageSettings::loadByEntityTypeBundle('node', 'bundle1')
->setDefaultLangcode('es')
->setLanguageAlterable(TRUE)
->save();
ContentLanguageSettings::loadByEntityTypeBundle('node', 'bundle2')
->setDefaultLangcode('es')
->setLanguageAlterable(TRUE)
->save();
$field_translation_map = [
1 => ['bundle1' => TRUE, 'bundle2' => TRUE],
2 => ['bundle1' => FALSE, 'bundle2' => FALSE],
3 => ['bundle1' => TRUE, 'bundle2' => FALSE],
];
for ($i = 1; $i < 4; $i++) {
$field_name = "field_name_$i";
FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'node',
'type' => 'string',
])->save();
foreach (['bundle1', 'bundle2'] as $bundle) {
FieldConfig::create([
'field_name' => $field_name,
'entity_type' => 'node',
'bundle' => $bundle,
'translatable' => $field_translation_map[$i][$bundle],
])->save();
}
}
$node1 = Node::create([
'title' => 'Test title bundle1',
'type' => 'bundle1',
'langcode' => 'es',
'field_name_1' => 'field name 1: es',
'field_name_2' => 'field name 2: es',
'field_name_3' => 'field name 3: es',
]);
$node1->save();
/** @var \Drupal\node\NodeInterface $translation */
$node1->addTranslation('fr', [
'title' => $node1->title->value,
'field_name_1' => 'field name 1: fr',
'field_name_3' => 'field name 3: fr',
])->save();
$node2 = Node::create([
'title' => 'Test title bundle2',
'type' => 'bundle2',
'langcode' => 'es',
'field_name_1' => 'field name 1: es',
'field_name_2' => 'field name 2: es',
'field_name_3' => 'field name 3: es',
]);
$node2->save();
$node2->addTranslation('fr', [
'title' => $node2->title->value,
'field_name_1' => 'field name 1: fr',
])->save();
$map = [
'nid' => 'nid',
'langcode' => 'langcode',
......@@ -298,11 +284,11 @@ public function testEntityFieldFilter() {
$this->executeView($view);
$expected = [
[
'nid' => $this->translationNodes[0]->id(),
'nid' => $node1->id(),
'langcode' => 'es',
],
[
'nid' => $this->translationNodes[1]->id(),
'nid' => $node2->id(),
'langcode' => 'es',
],
];
......@@ -315,11 +301,11 @@ public function testEntityFieldFilter() {
$this->executeView($view);
$expected = [
[
'nid' => $this->translationNodes[0]->id(),
'nid' => $node1->id(),
'langcode' => 'fr',
],
[
'nid' => $this->translationNodes[1]->id(),
'nid' => $node2->id(),
'langcode' => 'fr',
],
];
......@@ -332,19 +318,19 @@ public function testEntityFieldFilter() {
$this->executeView($view);
$expected = [
[
'nid' => $this->translationNodes[0]->id(),
'nid' => $node1->id(),
'langcode' => 'es',
],
[
'nid' => $this->translationNodes[0]->id(),
'nid' => $node1->id(),
'langcode' => 'fr',
],
[
'nid' => $this->translationNodes[1]->id(),
'nid' => $node2->id(),
'langcode' => 'es',
],
[
'nid' => $this->translationNodes[1]->id(),
'nid' => $node2->id(),
'langcode' => 'fr',
],
];
......@@ -365,16 +351,16 @@ public function testEntityFieldFilter() {
$this->executeView($view);
$expected = [
[
'nid' => $this->translationNodes[0]->id(),
'nid' => $node1->id(),
'langcode' => 'es',
],
[
'nid' => $this->translationNodes[1]->id(),
'nid' => $node2->id(),
'langcode' => 'es',
],
// Why is this one returned?
[
'nid' => $this->translationNodes[1]->id(),
'nid' => $node2->id(),
'langcode' => 'fr',
],
];
......@@ -387,7 +373,7 @@ public function testEntityFieldFilter() {
$this->executeView($view);
$expected = [
[
'nid' => $this->translationNodes[0]->id(),
'nid' => $node1->id(),
'langcode' => 'fr',
],
];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment