View source
<?php
namespace Drupal\Tests\views_bulk_operations\FunctionalJavaScript;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\views_bulk_operations\Form\ViewsBulkOperationsFormTrait;
class ViewsBulkOperationsBulkFormTest extends WebDriverTestBase {
use ViewsBulkOperationsFormTrait;
const TEST_NODE_COUNT = 15;
const TEST_VIEW_ID = 'views_bulk_operations_test';
protected $defaultTheme = 'stable';
protected $assertSession;
protected $page;
protected $selectedIndexes = [];
protected $testNodes = [];
protected $testViewParams;
public static $modules = [
'node',
'views',
'views_bulk_operations',
'views_bulk_operations_test',
];
protected function setUp() {
parent::setUp();
$this
->drupalCreateContentType([
'type' => 'page',
]);
for ($i = 0; $i <= self::TEST_NODE_COUNT; $i++) {
$this
->drupalCreateNode([
'type' => 'page',
'title' => 'Title ' . $i,
]);
}
$admin_user = $this
->drupalCreateUser([
'edit any page content',
'create page content',
'delete any page content',
]);
$this
->drupalLogin($admin_user);
$this->assertSession = $this
->assertSession();
$this->page = $this
->getSession()
->getPage();
$testViewConfig = \Drupal::service('config.factory')
->getEditable('views.view.' . self::TEST_VIEW_ID);
$config_data = $testViewConfig
->getRawData();
$this->testViewParams = [
'items_per_page' => $config_data['display']['default']['display_options']['pager']['options']['items_per_page'],
'path' => $config_data['display']['page_1']['display_options']['path'],
];
$config_data['display']['default']['display_options']['use_ajax'] = TRUE;
$testViewConfig
->setData($config_data);
$testViewConfig
->save();
$this
->drupalGet('/' . $this->testViewParams['path']);
}
public function testViewsBulkOperationsAjaxUi() {
$this->assertSession
->buttonExists('Simple test action');
for ($i = 0; $i < $this->testViewParams['items_per_page']; $i++) {
$this->assertSession
->fieldExists('edit-views-bulk-operations-bulk-form-' . $i);
}
foreach ([
0,
1,
3,
] as $selected_index) {
$this->selectedIndexes[] = $selected_index;
$this->page
->checkField('views_bulk_operations_bulk_form[' . $selected_index . ']');
}
$this->page
->clickLink('Go to next page');
$this->assertSession
->assertWaitOnAjaxRequest();
foreach ([
1,
2,
] as $selected_index) {
$this->selectedIndexes[] = $selected_index + $this->testViewParams['items_per_page'];
$this->page
->checkField('views_bulk_operations_bulk_form[' . $selected_index . ']');
}
$this->page
->pressButton('Simple test action');
foreach ($this->testNodes as $delta => $node) {
if (in_array($delta, $this->selectedIndexes, TRUE)) {
$this->assertSession
->pageTextContains(sprintf('Test action (preconfig: Test setting, label: %s)', $node
->label()));
}
else {
$this->assertSession
->pageTextNotContains(sprintf('Test action (preconfig: Test setting, label: %s)', $node
->label()));
}
}
$this->assertSession
->pageTextContains(sprintf('Action processing results: Test (%s)', count($this->selectedIndexes)));
}
public function testViewsBulkOperationsWithDynamicInsertion() {
$this->selectedIndexes = [
0,
1,
3,
];
foreach ($this->selectedIndexes as $selected_index) {
$this->page
->checkField('views_bulk_operations_bulk_form[' . $selected_index . ']');
}
$nodes = [];
for ($i = 100; $i < 100 + self::TEST_NODE_COUNT; $i++) {
$nodes[] = $this
->drupalCreateNode([
'type' => 'page',
'title' => 'Title ' . $i,
]);
}
$this->page
->pressButton('Simple test action');
foreach ($this->selectedIndexes as $index) {
$this->assertSession
->pageTextContains(sprintf('Test action (preconfig: Test setting, label: Title %s)', self::TEST_NODE_COUNT - $index));
}
$this->assertSession
->pageTextContains(sprintf('Action processing results: Test (%s)', count($this->selectedIndexes)));
}
}