View source
<?php
namespace Drupal\Tests\views_bulk_operations\Functional;
use Drupal\Tests\BrowserTestBase;
class ViewsBulkOperationsBulkFormTest extends BrowserTestBase {
public static $modules = [
'node',
'views',
'views_bulk_operations',
'views_bulk_operations_test',
];
protected function setUp() {
parent::setUp();
$this
->drupalCreateContentType([
'type' => 'page',
]);
$this->testNodes = [];
$time = REQUEST_TIME;
for ($i = 0; $i < 15; $i++) {
$time -= $i;
$this->testNodes[] = $this
->drupalCreateNode([
'type' => 'page',
'title' => 'Title ' . $i,
'sticky' => FALSE,
'created' => $time,
'changed' => $time,
]);
}
}
protected function assertBatchProcess() {
$current_url = $this
->getUrl();
$q = substr($current_url, strrpos($current_url, '/') + 1);
$this
->assertEquals('batch?', substr($q, 0, 6), 'We are on a Batch API page.');
preg_match('#id=([0-9]+)#', $q, $matches);
$batch_id = $matches[1];
do {
$this
->drupalGet('batch', [
'query' => [
'id' => $batch_id,
'op' => 'do_nojs',
],
]);
} while (FALSE);
$this
->drupalGet('batch', [
'query' => [
'id' => $batch_id,
'op' => 'finished',
],
]);
}
public function testViewsBulkOperationsBulkFormSimple() {
$assertSession = $this
->assertSession();
$this
->drupalGet('views-bulk-operations-test');
$first_form_element = $this
->xpath('//form/div[1][@id = :id]', [
':id' => 'edit-header',
]);
$this
->assertTrue($first_form_element, 'The views form edit header appears first.');
$edit = [];
for ($i = 0; $i < 4; $i++) {
$assertSession
->fieldExists('edit-views-bulk-operations-bulk-form-' . $i, NULL, format_string('The checkbox on row @row appears.', [
'@row' => $i,
]));
}
$this
->assertTrue(empty($this
->cssSelect('select[name=views_bulk_operations_advanced_test_action]')), t('Advanced action is not selectable.'));
$admin_user = $this
->drupalCreateUser([
'edit any page content',
]);
$this
->drupalLogin($admin_user);
$edit = [];
$selected = [
0,
2,
3,
];
foreach ($selected as $index) {
$edit["views_bulk_operations_bulk_form[{$index}]"] = TRUE;
}
$this
->drupalPostForm('views-bulk-operations-test', $edit, t('Simple test action'));
$testViewConfig = \Drupal::service('config.factory')
->get('views.view.views_bulk_operations_test');
$configData = $testViewConfig
->getRawData();
$preconfig_setting = $configData['display']['default']['display_options']['fields']['views_bulk_operations_bulk_form']['preconfiguration']['views_bulk_operations_simple_test_action']['preconfig'];
foreach ($selected as $index) {
$assertSession
->pageTextContains(sprintf('Test action (preconfig: %s, label: %s)', $preconfig_setting, $this->testNodes[$index]
->label()), sprintf('Action has been executed on node "%s".', $this->testNodes[$index]
->label()));
}
$edit = [
'select_all' => 1,
];
$this
->drupalPostForm(NULL, $edit, t('Simple test action'));
$assertSession
->pageTextContains(sprintf('Action processing results: Test (%d).', count($this->testNodes)), sprintf('Action has been executed on %d nodes.', count($this->testNodes)));
}
public function testViewsBulkOperationsBulkFormAdvanced() {
$assertSession = $this
->assertSession();
$admin_user = $this
->drupalCreateUser([
'edit any page content',
'execute advanced test action',
]);
$this
->drupalLogin($admin_user);
$edit = [
'action' => 'views_bulk_operations_simple_test_action',
];
$selected = [
0,
2,
];
foreach ($selected as $index) {
$edit["views_bulk_operations_bulk_form[{$index}]"] = TRUE;
}
$this
->drupalPostForm('views-bulk-operations-test-advanced', $edit, t('Apply to selected items'));
$assertSession
->pageTextContains(sprintf('Action processing results: Test (%d).', count($selected)), sprintf('Action has been executed on %d nodes.', count($selected)));
$edit = [
'action' => 'views_bulk_operations_advanced_test_action',
];
$selected = [
0,
1,
3,
];
foreach ($selected as $index) {
$edit["views_bulk_operations_bulk_form[{$index}]"] = TRUE;
}
$this
->drupalPostForm('views-bulk-operations-test-advanced', $edit, t('Apply to selected items'));
$assertSession
->fieldExists('edit-test-config', NULL, 'The configuration field appears.');
foreach ($selected as $index) {
$assertSession
->pageTextContains($this->testNodes[$index + 1]
->label());
}
$config_value = 'test value';
$edit = [
'test_config' => $config_value,
];
$this
->drupalPostForm(NULL, $edit, t('Apply'));
$this
->drupalPostForm(NULL, [], t('Execute action'));
$testViewConfig = \Drupal::service('config.factory')
->get('views.view.views_bulk_operations_test_advanced');
$configData = $testViewConfig
->getRawData();
$preconfig_setting = $configData['display']['default']['display_options']['fields']['views_bulk_operations_bulk_form']['preconfiguration']['views_bulk_operations_advanced_test_action']['test_preconfig'];
foreach ($selected as $index) {
$assertSession
->pageTextContains(sprintf('Test action (preconfig: %s, config: %s, label: %s)', $preconfig_setting, $config_value, $this->testNodes[$index + 1]
->label()));
}
$edit = [
'action' => 'views_bulk_operations_advanced_test_action',
'select_all' => 1,
];
$this
->drupalPostForm(NULL, $edit, t('Apply to selected items'));
$this
->drupalPostForm(NULL, [
'test_config' => 'unpublish',
], t('Apply'));
$this
->drupalPostForm(NULL, [], t('Execute action'));
$assertSession
->pageTextContains(sprintf('Action processing results: Test (%d).', count($this->testNodes) - 1), sprintf('Action has been executed on all %d nodes.', count($this->testNodes) - 1));
$this
->assertTrue(empty($this
->cssSelect('table.views-table tr')), t("The view doesn't show any results."));
}
public function testViewsBulkOperationsBulkFormPassing() {
$assertSession = $this
->assertSession();
$admin_user = $this
->drupalCreateUser([
'bypass node access',
]);
$this
->drupalLogin($admin_user);
$cases = [
[
'batch' => FALSE,
'selection' => TRUE,
'page' => 1,
],
[
'batch' => FALSE,
'selection' => FALSE,
],
[
'batch' => TRUE,
'batch_size' => 3,
'selection' => TRUE,
'page' => 1,
],
[
'batch' => TRUE,
'batch_size' => 7,
'selection' => TRUE,
],
[
'batch' => TRUE,
'batch_size' => 3,
'selection' => FALSE,
],
[
'batch' => TRUE,
'batch_size' => 7,
'selection' => FALSE,
],
];
$selected = [
0,
1,
3,
4,
];
$testViewConfig = \Drupal::service('config.factory')
->getEditable('views.view.views_bulk_operations_test_advanced');
$configData = $testViewConfig
->getRawData();
$configData['display']['default']['display_options']['pager']['options']['items_per_page'] = 5;
foreach ($cases as $case) {
$edit = [
'action' => 'views_bulk_operations_passing_test_action',
];
if ($case['selection']) {
foreach ($selected as $index) {
$edit["views_bulk_operations_bulk_form[{$index}]"] = TRUE;
}
}
else {
$edit['select_all'] = 1;
}
$configData['display']['default']['display_options']['pager']['options']['items_per_page']++;
$configData['display']['default']['display_options']['fields']['views_bulk_operations_bulk_form']['batch'] = $case['batch'];
if (isset($case['batch_size'])) {
$configData['display']['default']['display_options']['fields']['views_bulk_operations_bulk_form']['batch_size'] = $case['batch_size'];
}
$testViewConfig
->setData($configData);
$testViewConfig
->save();
$options = [];
if (!empty($case['page'])) {
$options['query'] = [
'page' => $case['page'],
];
}
$this
->drupalGet('views-bulk-operations-test-advanced', $options);
$this
->drupalPostForm(NULL, $edit, t('Apply to selected items'));
if ($case['batch']) {
if ($case['selection']) {
$total = count($selected);
}
else {
$total = count($this->testNodes) - 1;
}
$n_batches = ceil($total / $case['batch_size']);
for ($i = 0; $i < $n_batches; $i++) {
$processed = $i * $case['batch_size'];
$assertSession
->pageTextContains(sprintf('Processed %s of %s.', $processed, $total), 'The correct processed info message appears.');
}
}
$assertSession
->pageTextContains('Passed view results match the entity queue.');
}
}
}