Skip to content
Snippets Groups Projects
Unverified Commit 97f5efc2 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3041765 by claudiu.cristea: Convert FieldGroupRowsTest into a Kernel test

parent b5f683dc
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
<?php
namespace Drupal\Tests\views\Functional\Handler;
namespace Drupal\Tests\views\Kernel\Handler;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Render\RenderContext;
use Drupal\field\Entity\FieldConfig;
use Drupal\Tests\views\Functional\ViewTestBase;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
use Drupal\views\Views;
use Drupal\field\Entity\FieldStorageConfig;
......@@ -16,77 +18,65 @@
*
* @group views
*/
class FieldGroupRowsTest extends ViewTestBase {
class FieldGroupRowsTest extends ViewsKernelTestBase {
/**
* Views used by this test.
*
* @var array
* {@inheritdoc}
*/
public static $testViews = ['test_group_rows', 'test_ungroup_rows'];
/**
* Modules to enable.
*
* @var array
*/
public static $modules = ['node', 'field_test'];
public static $modules = [
'field',
'filter',
'node',
'text',
'user',
];
/**
* Field that will be created to test the group/ungroup rows functionality
*
* @var string
* {@inheritdoc}
*/
private $fieldName = 'field_group_rows';
protected function setUp($import_test_views = TRUE) {
parent::setUp($import_test_views);
// Create content type with unlimited text field.
$node_type = $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
// Create the unlimited text field.
$field_storage = FieldStorageConfig::create([
'field_name' => $this->fieldName,
'entity_type' => 'node',
'type' => 'text',
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
]);
$field_storage->save();
// Create an instance of the text field on the content type.
$field = [
'field_storage' => $field_storage,
'bundle' => $node_type->id(),
];
FieldConfig::create($field)->save();
}
public static $testViews = ['test_group_rows', 'test_ungroup_rows'];
/**
* Testing the "Grouped rows" functionality.
*/
public function testGroupRows() {
/** @var \Drupal\Core\Render\RendererInterface $renderer */
$renderer = \Drupal::service('renderer');
$edit = [
$this->installConfig(['filter']);
$this->installEntitySchema('node');
$this->installEntitySchema('user');
NodeType::create(['type' => 'page'])->save();
// Create a text with unlimited cardinality.
FieldStorageConfig::create([
'type' => 'text',
'entity_type' => 'node',
'field_name' => 'field_group_rows',
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
])->save();
FieldConfig::create([
'entity_type' => 'node',
'bundle' => 'page',
'field_name' => 'field_group_rows',
])->save();
Node::create([
'type' => 'page',
'title' => $this->randomMachineName(),
$this->fieldName => ['a', 'b', 'c'],
];
$this->drupalCreateNode($edit);
'field_group_rows' => ['a', 'b', 'c'],
])->save();
$renderer = $this->container->get('renderer');
$view = Views::getView('test_group_rows');
// Test grouped rows.
$this->executeView($view);
$output = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) {
return $view->field[$this->fieldName]->advancedRender($view->result[0]);
return $view->field['field_group_rows']->advancedRender($view->result[0]);
});
$this->assertEqual($output, 'a, b, c');
$this->assertEquals('a, b, c', $output);
// Change the group_rows checkbox to false.
$view = Views::getView('test_group_rows');
$view->setHandlerOption('default', 'field', $this->fieldName, 'group_rows', FALSE);
$view->setHandlerOption('default', 'field', 'field_group_rows', 'group_rows', FALSE);
// Test ungrouped rows.
$this->executeView($view);
......@@ -94,19 +84,19 @@ public function testGroupRows() {
$view->row_index = 0;
$output = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) {
return $view->field[$this->fieldName]->advancedRender($view->result[0]);
return $view->field['field_group_rows']->advancedRender($view->result[0]);
});
$this->assertEqual($output, 'a');
$this->assertEquals('a', $output);
$view->row_index = 1;
$output = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) {
return $view->field[$this->fieldName]->advancedRender($view->result[1]);
return $view->field['field_group_rows']->advancedRender($view->result[1]);
});
$this->assertEqual($output, 'b');
$this->assertEquals('b', $output);
$view->row_index = 2;
$output = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) {
return $view->field[$this->fieldName]->advancedRender($view->result[2]);
return $view->field['field_group_rows']->advancedRender($view->result[2]);
});
$this->assertEqual($output, 'c');
$this->assertEquals('c', $output);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment