View source
<?php
namespace Drupal\views\Tests\Handler;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Render\RenderContext;
use Drupal\views\Views;
class FieldGroupRowsTest extends HandlerTestBase {
public static $testViews = array(
'test_group_rows',
'test_ungroup_rows',
);
public static $modules = array(
'node',
'field_test',
);
private $fieldName = 'field_group_rows';
protected function setUp() {
parent::setUp();
$node_type = $this
->drupalCreateContentType(array(
'type' => 'page',
'name' => 'Basic page',
));
$field_storage = entity_create('field_storage_config', array(
'field_name' => $this->fieldName,
'entity_type' => 'node',
'type' => 'text',
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
));
$field_storage
->save();
$field = array(
'field_storage' => $field_storage,
'bundle' => $node_type
->id(),
);
entity_create('field_config', $field)
->save();
}
public function testGroupRows() {
$renderer = \Drupal::service('renderer');
$edit = array(
'title' => $this
->randomMachineName(),
$this->fieldName => array(
'a',
'b',
'c',
),
);
$this
->drupalCreateNode($edit);
$view = Views::getView('test_group_rows');
$this
->executeView($view);
$output = $renderer
->executeInRenderContext(new RenderContext(), function () use ($view) {
return $view->field[$this->fieldName]
->advancedRender($view->result[0]);
});
$this
->assertEqual($output, 'a, b, c');
$view = Views::getView('test_group_rows');
$view
->setHandlerOption('default', 'field', $this->fieldName, 'group_rows', FALSE);
$this
->executeView($view);
$view
->render();
$view->row_index = 0;
$output = $renderer
->executeInRenderContext(new RenderContext(), function () use ($view) {
return $view->field[$this->fieldName]
->advancedRender($view->result[0]);
});
$this
->assertEqual($output, 'a');
$view->row_index = 1;
$output = $renderer
->executeInRenderContext(new RenderContext(), function () use ($view) {
return $view->field[$this->fieldName]
->advancedRender($view->result[1]);
});
$this
->assertEqual($output, 'b');
$view->row_index = 2;
$output = $renderer
->executeInRenderContext(new RenderContext(), function () use ($view) {
return $view->field[$this->fieldName]
->advancedRender($view->result[2]);
});
$this
->assertEqual($output, 'c');
}
}