View source
<?php
namespace Drupal\Tests\views_bulk_operations\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\simpletest\NodeCreationTrait;
use Drupal\node\Entity\NodeType;
use Drupal\views\Views;
use Drupal\language\Entity\ConfigurableLanguage;
class ViewsBulkOperationsDataServiceTest extends KernelTestBase {
use NodeCreationTrait {
getNodeByTitle as drupalGetNodeByTitle;
createNode as drupalCreateNode;
}
protected $testNodesData;
protected $resultsCount = 0;
protected $vboDataService;
public static $modules = [
'user',
'node',
'field',
'content_translation',
'views_bulk_operations',
'views_bulk_operations_test',
'views',
'filter',
'language',
'text',
'action',
'system',
];
public function setUp() {
parent::setUp();
$this
->installConfig([
'filter',
'views_bulk_operations_test',
'language',
]);
$this
->installEntitySchema('user');
$this
->installEntitySchema('node');
$this
->installSchema('node', 'node_access');
$languages = [
'pl',
'es',
'it',
'fr',
'de',
];
$count_languages = count($languages);
for ($i = 0; $i < $count_languages; $i++) {
$language = ConfigurableLanguage::createFromLangcode($languages[$i]);
$language
->save();
}
$type = NodeType::create([
'type' => 'page',
'name' => 'page',
]);
$type
->save();
\Drupal::service('content_translation.manager')
->setEnabled('node', 'page', TRUE);
\Drupal::entityManager()
->clearCachedDefinitions();
$this->testNodesData = [];
$time = REQUEST_TIME;
for ($i = 0; $i < 10; $i++) {
$time -= $i;
$title = 'Title ' . $i;
$node = $this
->drupalCreateNode([
'type' => 'page',
'title' => $title,
'sticky' => FALSE,
'created' => $time,
'changed' => $time,
]);
$this->testNodesData[$node
->id()]['en'] = $title;
$this->resultsCount++;
$langcode = $languages[rand(0, $count_languages - 1)];
$title = 'Translated title ' . $langcode . ' ' . $i;
$translation = $node
->addTranslation($langcode, [
'title' => $title,
]);
$translation
->save();
$this->testNodesData[$node
->id()][$langcode] = $title;
$this->resultsCount++;
}
$this->vboDataService = $this->container
->get('views_bulk_operations.data');
}
public function testViewsbulkOperationsViewDataEntityGetter() {
$view = Views::getView('views_bulk_operations_test');
$view
->setDisplay('page_1');
$view
->setItemsPerPage(0);
$view
->setCurrentPage(0);
$view
->execute();
$test_data = $this->testNodesData;
foreach ($view->result as $row) {
$entity = $this->vboDataService
->getEntityDefault($row, 'none', $view);
$expected_label = $test_data[$entity
->id()][$entity
->language()
->getId()];
$this
->assertEquals($expected_label, $entity
->label(), 'Title matches');
if ($expected_label === $entity
->label()) {
unset($test_data[$entity
->id()][$entity
->language()
->getId()]);
if (empty($test_data[$entity
->id()])) {
unset($test_data[$entity
->id()]);
}
}
}
$this
->assertEmpty($test_data, 'All created entities and their translations were returned.');
}
}