View source
<?php
namespace Drupal\Tests\views\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Tests\BrowserTestBase;
use Drupal\views\Views;
class BulkFormTest extends BrowserTestBase {
protected static $modules = [
'node',
'action_bulk_test',
];
protected $defaultTheme = 'stark';
public function testBulkForm() {
$node_storage = $this->container
->get('entity_type.manager')
->getStorage('node');
$this
->drupalGet('test_bulk_form_empty');
$this
->assertSession()
->pageTextContains('This view is empty.');
$nodes = [];
for ($i = 0; $i < 10; $i++) {
$timestamp = REQUEST_TIME - $i;
$nodes[] = $this
->drupalCreateNode([
'title' => 'Node ' . $i,
'sticky' => FALSE,
'created' => $timestamp,
'changed' => $timestamp,
]);
}
$this
->drupalGet('test_bulk_form');
$this
->assertSession()
->elementExists('xpath', '//form/div[1][@id = "edit-header"]');
$this
->assertSession()
->fieldExists('edit-action');
$edit = [];
for ($i = 0; $i < 10; $i++) {
$this
->assertSession()
->fieldExists('edit-node-bulk-form-' . $i);
$edit["node_bulk_form[{$i}]"] = TRUE;
}
$this
->drupalCreateContentType([
'type' => 'page',
]);
$admin_user = $this
->drupalCreateUser([
'administer nodes',
'edit any page content',
'delete any page content',
]);
$this
->drupalLogin($admin_user);
$this
->drupalGet('test_bulk_form');
$edit += [
'action' => 'node_make_sticky_action',
];
$this
->submitForm($edit, 'Apply to selected items');
foreach ($nodes as $node) {
$changed_node = $node_storage
->load($node
->id());
$this
->assertTrue($changed_node
->isSticky(), new FormattableMarkup('Node @nid got marked as sticky.', [
'@nid' => $node
->id(),
]));
}
$this
->assertSession()
->pageTextContains('Make content sticky was applied to 10 items.');
$node = $node_storage
->load($nodes[0]
->id());
$this
->assertTrue($node
->isPublished(), 'The node is published.');
$edit = [
'node_bulk_form[0]' => TRUE,
'action' => 'node_unpublish_action',
];
$this
->submitForm($edit, 'Apply to selected items');
$this
->assertSession()
->pageTextContains('Unpublish content was applied to 1 item.');
$node_storage
->resetCache([
$node
->id(),
]);
$node = $node_storage
->load($node
->id());
$this
->assertFalse($node
->isPublished(), 'A single node has been unpublished.');
$node_storage
->resetCache([
$nodes[1]
->id(),
]);
$node = $node_storage
->load($nodes[1]
->id());
$this
->assertTrue($node
->isPublished(), 'An unchecked node is still published.');
$view = Views::getView('test_bulk_form');
$display =& $view->storage
->getDisplay('default');
$display['display_options']['fields']['node_bulk_form']['include_exclude'] = 'include';
$display['display_options']['fields']['node_bulk_form']['selected_actions']['node_make_sticky_action'] = 'node_make_sticky_action';
$display['display_options']['fields']['node_bulk_form']['selected_actions']['node_make_unsticky_action'] = 'node_make_unsticky_action';
$view
->save();
$this
->drupalGet('test_bulk_form');
$options = $this
->assertSession()
->selectExists('edit-action')
->findAll('css', 'option');
$this
->assertCount(2, $options);
$this
->assertSession()
->optionExists('edit-action', 'node_make_sticky_action');
$this
->assertSession()
->optionExists('edit-action', 'node_make_unsticky_action');
$view = Views::getView('test_bulk_form');
$display =& $view->storage
->getDisplay('default');
$display['display_options']['fields']['node_bulk_form']['include_exclude'] = 'exclude';
$view
->save();
$this
->drupalGet('test_bulk_form');
$this
->assertSession()
->optionNotExists('edit-action', 'node_make_sticky_action');
$this
->assertSession()
->optionNotExists('edit-action', 'node_make_unsticky_action');
$this
->drupalGet('test_bulk_form');
$this
->assertSession()
->elementTextEquals('xpath', '//label[@for="edit-action"]', 'Action');
$view = Views::getView('test_bulk_form');
$display =& $view->storage
->getDisplay('default');
$display['display_options']['fields']['node_bulk_form']['action_title'] = 'Test title';
$view
->save();
$this
->drupalGet('test_bulk_form');
$this
->assertSession()
->elementTextEquals('xpath', '//label[@for="edit-action"]', 'Test title');
$this
->drupalGet('test_bulk_form');
$edit = [];
for ($i = 0; $i < 5; $i++) {
$edit["node_bulk_form[{$i}]"] = TRUE;
}
$edit += [
'action' => 'node_delete_action',
];
$this
->submitForm($edit, 'Apply to selected items');
$this
->assertSession()
->elementNotExists('xpath', '//div[contains(@class, "messages--status")]');
$this
->submitForm([], 'Delete');
$this
->assertSession()
->pageTextContains('Deleted 5 content items.');
$this
->assertSession()
->addressEquals('test_bulk_form');
$this
->drupalGet('test_bulk_form');
$link = $this
->getSession()
->getPage()
->findLink($nodes[6]
->label());
$checkbox = $link
->getParent()
->getParent()
->find('css', 'input');
$nodes[6]
->delete();
$edit = [
$checkbox
->getAttribute('name') => TRUE,
'action' => 'node_delete_action',
];
$this
->submitForm($edit, 'Apply to selected items');
$this
->assertSession()
->addressEquals('test_bulk_form');
$this
->assertSession()
->elementNotExists('xpath', '//div[contains(@class, "messages--status")]');
$this
->drupalGet('test_bulk_form');
$nodes[7]
->delete();
$edit = [
'node_bulk_form[0]' => TRUE,
'node_bulk_form[1]' => TRUE,
'action' => 'node_delete_action',
];
$this
->submitForm($edit, 'Apply to selected items');
$this
->assertSession()
->elementNotExists('xpath', '//div[contains(@class, "messages--status")]');
$this
->submitForm([], 'Delete');
$this
->assertSession()
->pageTextContains('Deleted 1 content item.');
$this
->drupalGet('test_bulk_form');
foreach ($nodes as $key => $node) {
$node
->delete();
}
$edit = [
'node_bulk_form[0]' => TRUE,
'action' => 'node_delete_action',
];
$this
->submitForm($edit, 'Apply to selected items');
$this
->assertSession()
->pageTextContains('No content selected.');
}
}