View source
<?php
namespace Drupal\Tests\views_bulk_operations\Functional;
use Drupal\Component\Render\FormattableMarkup;
class ViewsBulkOperationsBulkFormTest extends ViewsBulkOperationsFunctionalTestBase {
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
->assertNotEmpty($first_form_element, 'The views form edit header appears first.');
for ($i = 0; $i < 4; $i++) {
$checkbox_selector = 'edit-views-bulk-operations-bulk-form-' . $i;
$assertSession
->fieldExists($checkbox_selector, NULL, new FormattableMarkup('The checkbox on row @row appears.', [
'@row' => $i,
]));
$assertSession
->elementTextContains('css', "label[for={$checkbox_selector}]", $this->testNodes[$i]
->label());
}
$this
->assertEmpty($this
->cssSelect('input[value=views_bulk_operations_advanced_test_action]'), t('Advanced action is not selectable.'));
$admin_user = $this
->drupalCreateUser([
'edit any page content',
]);
$this
->drupalLogin($admin_user);
$selected = [
0,
2,
3,
];
$this
->executeAction('views-bulk-operations-test', t('Simple test action'), $selected);
$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']['selected_actions'][0]['preconfiguration']['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()));
}
$selected = [
0,
1,
2,
3,
];
$data = [
'select_all' => 1,
];
$this
->executeAction(NULL, t('Simple test action'), $selected, $data);
$assertSession
->pageTextContains(sprintf('Action processing results: Test (%d).', self::TEST_NODE_COUNT), sprintf('Action has been executed on %d nodes.', self::TEST_NODE_COUNT));
}
public function testViewsBulkOperationsBulkFormAdvanced() {
$assertSession = $this
->assertSession();
$admin_user = $this
->drupalCreateUser([
'edit any page content',
'execute advanced test action',
]);
$this
->drupalLogin($admin_user);
$selected = [
0,
2,
];
$data = [
'action' => 0,
];
$this
->executeAction('views-bulk-operations-test-advanced', t('Apply to selected items'), $selected, $data);
$assertSession
->pageTextContains(sprintf('Action processing results: Test (%d).', count($selected)), sprintf('Action has been executed on %d nodes.', count($selected)));
$selected = [
0,
1,
3,
];
$data = [
'action' => 1,
];
$this
->executeAction('views-bulk-operations-test-advanced', t('Apply to selected items'), $selected, $data);
$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']['selected_actions'][1]['preconfiguration']['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' => 1,
'select_all' => 1,
];
foreach ([
0,
2,
] as $index) {
$edit["views_bulk_operations_bulk_form[{$index}]"] = TRUE;
}
$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('Custom processing message: Test (%d).', count($this->testNodes) - 3), sprintf('Action has been executed on all %d nodes.', count($this->testNodes) - 3));
$this
->assertNotEmpty(count($this
->cssSelect('table.vbo-table tbody tr')) === 2, "The view shows only excluded 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();
$items_per_page = 5;
foreach ($cases as $case) {
$items_per_page++;
$edit = [
'action' => 2,
];
if ($case['selection']) {
foreach ($selected as $index) {
$edit["views_bulk_operations_bulk_form[{$index}]"] = TRUE;
}
}
else {
$edit['select_all'] = 1;
for ($i = 0; $i < $items_per_page; $i++) {
$edit["views_bulk_operations_bulk_form[{$i}]"] = TRUE;
}
}
$configData['display']['default']['display_options']['pager']['options']['items_per_page'] = $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.');
}
}
public function testActionCorePreconfig() {
$assertSession = $this
->assertSession();
$testViewConfig = \Drupal::service('config.factory')
->getEditable('views.view.views_bulk_operations_test');
$configData = $testViewConfig
->getRawData();
$preconfig =& $configData['display']['default']['display_options']['fields']['views_bulk_operations_bulk_form']['selected_actions'][0]['preconfiguration'];
$preconfig['add_confirmation'] = TRUE;
$testViewConfig
->setData($configData);
$testViewConfig
->save();
$this
->drupalGet('views-bulk-operations-test');
$admin_user = $this
->drupalCreateUser([
'edit any page content',
]);
$this
->drupalLogin($admin_user);
$selection = [
0,
2,
3,
];
$label = $preconfig['label_override'];
$this
->executeAction('views-bulk-operations-test', t('Simple test action'), $selection);
$assertSession
->pageTextContains(sprintf('Are you sure you wish to perform "%s" action on %d entities?', $label, count($selection)));
}
}