Commit 098ef29c authored by Marcin Grabias's avatar Marcin Grabias
Browse files

Improved JS test coverage.

parent ccb18215
Loading
Loading
Loading
Loading
+95 −27
Original line number Diff line number Diff line
@@ -17,7 +17,7 @@ class ViewsBulkOperationsBulkFormTest extends WebDriverTestBase {

  private const TEST_NODE_COUNT = 15;

  private const TEST_VIEW_ID = 'views_bulk_operations_test';
  private const TEST_VIEW_ID = 'views_bulk_operations_test_advanced';

  /**
   * {@inheritdoc}
@@ -82,13 +82,19 @@ class ViewsBulkOperationsBulkFormTest extends WebDriverTestBase {
        'type' => 'page',
        'title' => 'Title ' . $i,
        'created' => 1000 + self::TEST_NODE_COUNT - $i,
        'sticky' => $i % 2,
      ]);
    }

    // Sort nodes as in the view: sticky -> created.
    $this->sortNodes('ASC', $this->testNodes);

    $admin_user = $this->drupalCreateUser(
      [
        'edit any page content',
        'create page content',
        'delete any page content',
        'execute advanced test action',
      ]);
    $this->drupalLogin($admin_user);

@@ -105,54 +111,114 @@ class ViewsBulkOperationsBulkFormTest extends WebDriverTestBase {
    ];

    // Enable AJAX on the view.
    // Disable VBO batching and pager offset as that's tested elsewhere.
    $config_data['display']['default']['display_options']['use_ajax'] = TRUE;
    $config_data['display']['default']['display_options']['fields']['views_bulk_operations_bulk_form']['batch'] = FALSE;
    $config_data['display']['default']['display_options']['pager']['options']['offset'] = 0;
    $testViewConfig->setData($config_data);
    $testViewConfig->save();

    $this->drupalGet('/' . $this->testViewParams['path']);
  }

  /**
   * Helper function to sort nodes depending on exposed filter value.
   */
  private function sortNodes(string $sticky_order, array &$nodes): void {
    usort($nodes, function ($node1, $node2) use ($sticky_order) {
      if ($node1->get('sticky')->value === $node2->get('sticky')->value) {
        return $node2->get('created')->value - $node1->get('created')->value;
      }
      if ($sticky_order === 'DESC') {
        return $node2->get('sticky')->value - $node1->get('sticky')->value;
      }
      elseif ($sticky_order === 'ASC') {
        return $node1->get('sticky')->value - $node2->get('sticky')->value;
      }
    });
  }

  /**
   * Tests the VBO bulk form without dynamic insertion.
   */
  public function testViewsBulkOperationsAjaxUi(): void {
    // Make sure a checkbox appears on all rows and the button exists.
    $this->assertSession->buttonExists('Simple test action');
    // Make sure a checkbox appears on all rows.
    for ($i = 0; $i < $this->testViewParams['items_per_page']; $i++) {
      $this->assertSession->fieldExists('edit-views-bulk-operations-bulk-form-' . $i);
    }

    // Select some items on the first page.
    foreach ([0, 1, 3] as $selected_index) {
      $this->selectedIndexes[] = $selected_index;
      $this->page->checkField('views_bulk_operations_bulk_form[' . $selected_index . ']');
    // For better performance, line up tests that don't modify nodes but
    // just check if selection and processing works correctly.
    $this->testSelectionPersists();

  }

  /**
   * Test if selection persists on view pages.
   */
  private function testSelectionPersists() {
    $selected_ids = [];

    $page_selections = [
      [0, 1, 3],
      [1, 2],
    ];

    foreach ($page_selections as $page => $selection) {
      foreach ($selection as $page_index) {
        $selected_index = $page * $this->testViewParams['items_per_page'] + $page_index;
        $selected_indexes[$selected_index] = $selected_index;
        $node_id = $this->testNodes[$selected_index]->id();
        $selected_ids[$node_id] = $node_id;
        $this->page->checkField('views_bulk_operations_bulk_form[' . $page_index . ']');
      }

    // Go to the next page and select some more.
      $this->page->clickLink('Go to next page');
      $this->assertSession->assertWaitOnAjaxRequest();
    foreach ([1, 2] as $selected_index) {
      // This is page one so indexes are incremented by page count and
      // checkbox selectors start from 0 again.
      $this->selectedIndexes[] = $selected_index + $this->testViewParams['items_per_page'];
      $this->page->checkField('views_bulk_operations_bulk_form[' . $selected_index . ']');
    }

    // Change sort order, check if checkboxes are still checked.
    $this->page->selectFieldOption('sort_order', 'DESC');
    $this->page->pressButton('Apply');
    $this->assertSession->assertWaitOnAjaxRequest();

    // We should be back to page 0 now with different sorting applied
    // and selection still persisting.
    $this->sortNodes('DESC', $this->testNodes);
    $npages = (int) \ceil(self::TEST_NODE_COUNT / $this->testViewParams['items_per_page']);
    for ($page = 0;; $page++) {
      for ($i = 0; $i < $this->testViewParams['items_per_page']; $i++) {
        $view_node_index = $page * $this->testViewParams['items_per_page'] + $i;
        $node = $this->testNodes[$view_node_index];
        if (\array_key_exists($node->id(), $selected_ids)) {
          $this->assertTrue($this->page->hasCheckedField($node->label()), sprintf('%s should be still checked.', $node->label()));
        }
        else {
          $this->assertTrue($this->page->hasUncheckedField($node->label()), sprintf('%s should be still unchecked.', $node->label()));
        }
      }
      $this->createScreenshot('/var/www/html/page_' . $page . '.jpg');
      if ($page + 1 === $npages) {
        break;
      }
      $this->page->clickLink('Go to next page');
      $this->assertSession->assertWaitOnAjaxRequest();
    }

    // Execute test operation.
    $this->page->pressButton('Simple test action');
    $this->page->selectFieldOption('action', 'Simple test action');
    $this->page->pressButton('Apply to selected items');

    // Assert if only the selected nodes were processed.
    foreach ($this->testNodes as $delta => $node) {
      if (\in_array($delta, $this->selectedIndexes, TRUE)) {
    foreach ($this->testNodes as $node) {
      if (\array_key_exists($node->id(), $selected_ids)) {
        $this->assertSession->pageTextContains(\sprintf('Test action (label: %s)', $node->label()));
      }
      else {
        $this->assertSession->pageTextNotContains(\sprintf('Test action (label: %s)', $node->label()));
      }
    }
    $this->assertSession->pageTextContains(\sprintf('Action processing results: Test (%s)', \count($this->selectedIndexes)));

    $this->assertSession->pageTextContains(\sprintf('Action processing results: Test (%s)', \count($selected_ids)));
  }

  /**
@@ -162,13 +228,13 @@ class ViewsBulkOperationsBulkFormTest extends WebDriverTestBase {
   */
  public function testViewsBulkOperationsWithDynamicInsertion(): void {

    $this->selectedIndexes = [0, 1, 3];
    $selected_indexes = [0, 1, 3];

    foreach ($this->selectedIndexes as $selected_index) {
    foreach ($selected_indexes as $selected_index) {
      $this->page->checkField('views_bulk_operations_bulk_form[' . $selected_index . ']');
    }

    // Insert nodes that .
    // Insert nodes that will be displayed before the previous ones.
    for ($i = 0; $i < self::TEST_NODE_COUNT; $i++) {
      $this->drupalCreateNode([
        'type' => 'page',
@@ -177,12 +243,14 @@ class ViewsBulkOperationsBulkFormTest extends WebDriverTestBase {
      ]);
    }

    $this->page->pressButton('Simple test action');
    $this->page->selectFieldOption('action', 'Simple test action');
    $this->page->pressButton('Apply to selected items');

    foreach ($this->selectedIndexes as $index) {
      $this->assertSession->pageTextContains(\sprintf('Test action (label: Title %s)', $index));
    foreach ($selected_indexes as $index) {
      $node = $this->testNodes[$index];
      $this->assertSession->pageTextContains(\sprintf('Test action (label: %s)', $node->label()));
    }
    $this->assertSession->pageTextContains(\sprintf('Action processing results: Test (%s)', \count($this->selectedIndexes)));
    $this->assertSession->pageTextContains(\sprintf('Action processing results: Test (%s)', \count($selected_indexes)));

    // Check that the view now actually contains the new nodes in place of the
    // previously displayed ones.
+105 −85
Original line number Diff line number Diff line
@@ -12,85 +12,36 @@ description: ''
tag: ''
base_table: node_field_data
base_field: nid
core: 8.x
display:
  default:
    display_plugin: default
    id: default
    display_title: Master
    display_plugin: default
    position: 0
    display_options:
      access:
        type: perm
      cache:
        type: none
        options: {  }
      query:
        type: views_query
        options:
          disable_sql_rewrite: false
          distinct: false
          replica: false
          query_comment: ''
          query_tags: {  }
      exposed_form:
        type: basic
        options:
          submit_button: Apply
          reset_button: false
          reset_button_label: Reset
          exposed_sorts_label: 'Sort by'
          expose_sort_order: true
          sort_asc_label: Asc
          sort_desc_label: Desc
      pager:
        type: mini
        options:
          items_per_page: 4
          offset: 1
          id: 0
          total_pages: null
          tags:
            previous: ‹‹
            next: ››
          expose:
            items_per_page: false
            items_per_page_label: 'Items per page'
            items_per_page_options: '5, 10, 25, 50'
            items_per_page_options_all: false
            items_per_page_options_all_label: '- All -'
            offset: false
            offset_label: Offset
      style:
        type: table
      row:
        type: fields
      title: 'Views Bulk Operations Test'
      fields:
        title:
          id: title
          table: node_field_data
          field: title
          relationship: none
          group_type: group
          admin_label: ''
          entity_type: node
          entity_field: title
          plugin_id: field
          label: Title
          exclude: false
          alter:
            alter_text: false
            make_link: false
            absolute: false
            trim: false
            word_boundary: false
            ellipsis: false
            strip_tags: false
            trim: false
            html: false
          hide_empty: false
          empty_zero: false
          settings:
            link_to_entity: true
          plugin_id: field
          relationship: none
          group_type: group
          admin_label: ''
          label: Title
          exclude: false
          element_type: ''
          element_class: ''
          element_label_type: ''
@@ -100,9 +51,13 @@ display:
          element_wrapper_class: ''
          element_default_classes: true
          empty: ''
          hide_empty: false
          empty_zero: false
          hide_alter_empty: true
          click_sort_column: value
          type: string
          settings:
            link_to_entity: true
          group_column: value
          group_columns: {  }
          group_rows: true
@@ -120,6 +75,7 @@ display:
          relationship: none
          group_type: group
          admin_label: ''
          plugin_id: views_bulk_operations_bulk_form
          label: 'Views bulk operations'
          exclude: false
          alter:
@@ -167,14 +123,14 @@ display:
          ajax_loader: false
          buttons: false
          action_title: Action
          clear_on_exposed: true
          clear_on_exposed: false
          force_selection_info: false
          selected_actions:
            -
              action_id: views_bulk_operations_simple_test_action
              preconfiguration:
                label_override: 'Simple test action'
                add_confirmation: false
                label_override: 'Simple test action'
            -
              action_id: views_bulk_operations_advanced_test_action
              preconfiguration:
@@ -184,7 +140,72 @@ display:
              action_id: views_bulk_operations_passing_test_action
            -
              action_id: views_bulk_operations_test_action_v2
          plugin_id: views_bulk_operations_bulk_form
      pager:
        type: mini
        options:
          offset: 1
          items_per_page: 4
          total_pages: null
          id: 0
          tags:
            next: ››
            previous: ‹‹
          expose:
            items_per_page: false
            items_per_page_label: 'Items per page'
            items_per_page_options: '5, 10, 25, 50'
            items_per_page_options_all: false
            items_per_page_options_all_label: '- All -'
            offset: false
            offset_label: Offset
      exposed_form:
        type: basic
        options:
          submit_button: Apply
          reset_button: false
          reset_button_label: Reset
          exposed_sorts_label: 'Sort by'
          expose_sort_order: true
          sort_asc_label: Asc
          sort_desc_label: Desc
      access:
        type: perm
      cache:
        type: none
        options: {  }
      empty: {  }
      sorts:
        sticky:
          id: sticky
          table: node_field_data
          field: sticky
          relationship: none
          group_type: group
          admin_label: ''
          entity_type: node
          entity_field: sticky
          plugin_id: standard
          order: ASC
          expose:
            label: 'Sticky at top of lists'
            field_identifier: sticky
          exposed: true
        created:
          id: created
          table: node_field_data
          field: created
          relationship: none
          group_type: group
          admin_label: ''
          entity_type: node
          entity_field: created
          plugin_id: date
          order: DESC
          expose:
            label: ''
          exposed: false
          granularity: second
      arguments: {  }
      filters:
        status:
          id: status
@@ -193,6 +214,9 @@ display:
          relationship: none
          group_type: group
          admin_label: ''
          entity_type: node
          entity_field: status
          plugin_id: boolean
          operator: '='
          value: '1'
          group: 1
@@ -203,6 +227,8 @@ display:
            description: ''
            use_operator: false
            operator: ''
            operator_limit_selection: false
            operator_list: {  }
            identifier: ''
            required: false
            remember: false
@@ -221,31 +247,21 @@ display:
            default_group: All
            default_group_multiple: {  }
            group_items: {  }
          entity_type: node
          entity_field: status
          plugin_id: boolean
      sorts:
        created:
          id: created
          table: node_field_data
          field: created
          relationship: none
          group_type: group
          admin_label: ''
          order: DESC
          exposed: false
          expose:
            label: ''
          granularity: second
          entity_type: node
          entity_field: created
          plugin_id: date
      title: 'Views Bulk Operations Test'
      style:
        type: table
      row:
        type: fields
      query:
        type: views_query
        options:
          query_comment: ''
          disable_sql_rewrite: false
          distinct: false
          replica: false
          query_tags: {  }
      relationships: {  }
      header: {  }
      footer: {  }
      empty: {  }
      relationships: {  }
      arguments: {  }
      display_extenders: {  }
    cache_metadata:
      max-age: 0
@@ -253,13 +269,15 @@ display:
        - 'languages:language_content'
        - 'languages:language_interface'
        - url.query_args
        - 'url.query_args:sort_by'
        - 'url.query_args:sort_order'
        - 'user.node_grants:view'
        - user.permissions
      tags: {  }
  page_1:
    display_plugin: page
    id: page_1
    display_title: Page
    display_plugin: page
    position: 1
    display_options:
      display_extenders: {  }
@@ -270,6 +288,8 @@ display:
        - 'languages:language_content'
        - 'languages:language_interface'
        - url.query_args
        - 'url.query_args:sort_by'
        - 'url.query_args:sort_order'
        - 'user.node_grants:view'
        - user.permissions
      tags: {  }