BulkFormTest.php 5.97 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php

/**
 * @file
 * Contains \Drupal\action\Tests\BulkFormTest.
 */

namespace Drupal\action\Tests;

use Drupal\simpletest\WebTestBase;
11
use Drupal\views\Views;
12 13 14 15

/**
 * Tests the views bulk form test.
 *
16
 * @group action
17 18 19 20 21
 * @see \Drupal\action\Plugin\views\field\BulkForm
 */
class BulkFormTest extends WebTestBase {

  /**
22
   * Modules to install.
23 24 25
   *
   * @var array
   */
26
  public static $modules = array('node', 'action_bulk_test');
27 28 29 30 31

  /**
   * Tests the bulk form.
   */
  public function testBulkForm() {
32 33
    $node_storage = $this->container->get('entity.manager')->getStorage('node');

34 35 36 37 38
    // First, test an empty bulk form with the default style plugin to make sure
    // the empty region is rendered correctly.
    $this->drupalGet('test_bulk_form_empty');
    $this->assertText(t('This view is empty.'), 'Empty text found on empty bulk form.');

39 40
    $nodes = array();
    for ($i = 0; $i < 10; $i++) {
41 42 43 44 45 46 47 48
      // Ensure nodes are sorted in the same order they are inserted in the
      // array.
      $timestamp = REQUEST_TIME - $i;
      $nodes[] = $this->drupalCreateNode(array(
        'sticky' => FALSE,
        'created' => $timestamp,
        'changed' => $timestamp,
      ));
49 50 51 52
    }

    $this->drupalGet('test_bulk_form');

53
    // Test that the views edit header appears first.
54
    $first_form_element = $this->xpath('//form/div[1][@id = :id]', array(':id' => 'edit-header'));
55 56
    $this->assertTrue($first_form_element, 'The views form edit header appears first.');

57 58 59 60 61
    $this->assertFieldById('edit-action', NULL, 'The action select field appears.');

    // Make sure a checkbox appears on all rows.
    $edit = array();
    for ($i = 0; $i < 10; $i++) {
62 63
      $this->assertFieldById('edit-node-bulk-form-' . $i, NULL, format_string('The checkbox on row @row appears.', array('@row' => $i)));
      $edit["node_bulk_form[$i]"] = TRUE;
64 65
    }

66 67 68
    // Log in as a user with 'administer nodes' permission to have access to the
    // bulk operation.
    $this->drupalCreateContentType(['type' => 'page']);
69
    $admin_user = $this->drupalCreateUser(['administer nodes', 'edit any page content', 'delete any page content']);
70 71 72 73
    $this->drupalLogin($admin_user);

    $this->drupalGet('test_bulk_form');

74 75
    // Set all nodes to sticky and check that.
    $edit += array('action' => 'node_make_sticky_action');
76
    $this->drupalPostForm(NULL, $edit, t('Apply'));
77 78

    foreach ($nodes as $node) {
79
      $changed_node = $node_storage->load($node->id());
80
      $this->assertTrue($changed_node->isSticky(), format_string('Node @nid got marked as sticky.', array('@nid' => $node->id())));
81 82
    }

83
    $this->assertText('Make content sticky was applied to 10 items.');
84 85

    // Unpublish just one node.
86
    $node = $node_storage->load($nodes[0]->id());
87
    $this->assertTrue($node->isPublished(), 'The node is published.');
88

89
    $edit = array('node_bulk_form[0]' => TRUE, 'action' => 'node_unpublish_action');
90
    $this->drupalPostForm(NULL, $edit, t('Apply'));
91

92
    $this->assertText('Unpublish content was applied to 1 item.');
93 94

    // Load the node again.
95 96
    $node_storage->resetCache(array($node->id()));
    $node = $node_storage->load($node->id());
97
    $this->assertFalse($node->isPublished(), 'A single node has been unpublished.');
98 99

    // The second node should still be published.
100 101
    $node_storage->resetCache(array($nodes[1]->id()));
    $node = $node_storage->load($nodes[1]->id());
102
    $this->assertTrue($node->isPublished(), 'An unchecked node is still published.');
103 104

    // Set up to include just the sticky actions.
105
    $view = Views::getView('test_bulk_form');
106
    $display = &$view->storage->getDisplay('default');
107 108 109
    $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';
110 111 112 113 114 115 116 117 118
    $view->save();

    $this->drupalGet('test_bulk_form');
    $options = $this->xpath('//select[@id=:id]/option', array(':id' => 'edit-action'));
    $this->assertEqual(count($options), 2);
    $this->assertOption('edit-action', 'node_make_sticky_action');
    $this->assertOption('edit-action', 'node_make_unsticky_action');

    // Set up to exclude the sticky actions.
119
    $view = Views::getView('test_bulk_form');
120
    $display = &$view->storage->getDisplay('default');
121
    $display['display_options']['fields']['node_bulk_form']['include_exclude'] = 'exclude';
122 123 124 125 126
    $view->save();

    $this->drupalGet('test_bulk_form');
    $this->assertNoOption('edit-action', 'node_make_sticky_action');
    $this->assertNoOption('edit-action', 'node_make_unsticky_action');
127 128 129 130 131 132 133

    // Check the default title.
    $this->drupalGet('test_bulk_form');
    $result = $this->xpath('//label[@for="edit-action"]');
    $this->assertEqual('With selection', (string) $result[0]);

    // Setup up a different bulk form title.
134
    $view = Views::getView('test_bulk_form');
135
    $display = &$view->storage->getDisplay('default');
136
    $display['display_options']['fields']['node_bulk_form']['action_title'] = 'Test title';
137 138 139 140 141
    $view->save();

    $this->drupalGet('test_bulk_form');
    $result = $this->xpath('//label[@for="edit-action"]');
    $this->assertEqual('Test title', (string) $result[0]);
142 143 144 145 146 147 148 149 150

    $this->drupalGet('test_bulk_form');
    // Call the node delete action.
    $edit = array();
    for ($i = 0; $i < 5; $i++) {
      $edit["node_bulk_form[$i]"] = TRUE;
    }
    $edit += array('action' => 'node_delete_action');
    $this->drupalPostForm(NULL, $edit, t('Apply'));
151 152 153 154
    // Make sure we don't show an action message while we are still on the
    // confirmation page.
    $errors = $this->xpath('//div[contains(@class, "messages--status")]');
    $this->assertFalse($errors, 'No action message shown.');
155
    $this->drupalPostForm(NULL, array(), t('Delete'));
156
    $this->assertText(t('Deleted 5 posts.'));
157 158
    // Check if we got redirected to the original page.
    $this->assertUrl('test_bulk_form');
159 160 161
  }

}