SearchCommentCountToggleTest.php 4.48 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Definition of Drupal\search\Tests\SearchCommentCountToggleTest.
 */

namespace Drupal\search\Tests;

10 11
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;

12 13 14 15 16 17 18 19 20 21 22
/**
 * Tests that comment count display toggles properly on comment status of node
 *
 * Issue 537278
 *
 * - Nodes with comment status set to Open should always how comment counts
 * - Nodes with comment status set to Closed should show comment counts
 *     only when there are comments
 * - Nodes with comment status set to Hidden should never show comment counts
 */
class SearchCommentCountToggleTest extends SearchTestBase {
23 24 25 26 27 28 29 30

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('comment');

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
  // Requires node types, comment config, filter formats.
  protected $profile = 'standard';

  protected $searching_user;
  protected $searchable_nodes;

  public static function getInfo() {
    return array(
      'name' => 'Comment count toggle',
      'description' => 'Verify that comment count display toggles properly on comment status of node.',
      'group' => 'Search',
    );
  }

  function setUp() {
46
    parent::setUp();
47 48 49 50

    // Create searching user.
    $this->searching_user = $this->drupalCreateUser(array('search content', 'access content', 'access comments', 'skip comment approval'));

51 52 53
    // Login with sufficient privileges.
    $this->drupalLogin($this->searching_user);

54 55
    // Add a comment field.
    $this->container->get('comment.manager')->addDefaultField('node', 'article');
56
    // Create initial nodes.
57
    $node_params = array('type' => 'article', 'body' => array(array('value' => 'SearchCommentToggleTestCase')));
58 59 60 61 62 63 64

    $this->searchable_nodes['1 comment'] = $this->drupalCreateNode($node_params);
    $this->searchable_nodes['0 comments'] = $this->drupalCreateNode($node_params);

    // Create a comment array
    $edit_comment = array();
    $edit_comment['subject'] = $this->randomName();
65
    $edit_comment['comment_body[0][value]'] = $this->randomName();
66 67

    // Post comment to the test node with comment
68
    $this->drupalPostForm('comment/reply/node/' . $this->searchable_nodes['1 comment']->id() . '/comment', $edit_comment, t('Save'));
69 70

    // First update the index. This does the initial processing.
71
    $this->container->get('plugin.manager.search')->createInstance('node_search')->updateIndex();
72 73 74 75 76 77 78 79 80 81 82 83 84

    // Then, run the shutdown function. Testing is a unique case where indexing
    // and searching has to happen in the same request, so running the shutdown
    // function manually is needed to finish the indexing process.
    search_update_totals();
  }

  /**
   * Verify that comment count display toggles properly on comment status of node
   */
  function testSearchCommentCountToggle() {
    // Search for the nodes by string in the node body.
    $edit = array(
85
      'search_block_form' => "'SearchCommentToggleTestCase'",
86 87 88
    );

    // Test comment count display for nodes with comment status set to Open
89
    $this->drupalPostForm('', $edit, t('Search'));
90 91
    $this->assertText(t('0 comments'), 'Empty comment count displays for nodes with comment status set to Open');
    $this->assertText(t('1 comment'), 'Non-empty comment count displays for nodes with comment status set to Open');
92 93

    // Test comment count display for nodes with comment status set to Closed
94
    $this->searchable_nodes['0 comments']->set('comment', CommentItemInterface::CLOSED);
95
    $this->searchable_nodes['0 comments']->save();
96
    $this->searchable_nodes['1 comment']->set('comment', CommentItemInterface::CLOSED);
97
    $this->searchable_nodes['1 comment']->save();
98

99
    $this->drupalPostForm('', $edit, t('Search'));
100 101
    $this->assertNoText(t('0 comments'), 'Empty comment count does not display for nodes with comment status set to Closed');
    $this->assertText(t('1 comment'), 'Non-empty comment count displays for nodes with comment status set to Closed');
102 103

    // Test comment count display for nodes with comment status set to Hidden
104
    $this->searchable_nodes['0 comments']->set('comment', CommentItemInterface::HIDDEN);
105
    $this->searchable_nodes['0 comments']->save();
106
    $this->searchable_nodes['1 comment']->set('comment', CommentItemInterface::HIDDEN);
107
    $this->searchable_nodes['1 comment']->save();
108

109
    $this->drupalPostForm('', $edit, t('Search'));
110 111
    $this->assertNoText(t('0 comments'), 'Empty comment count does not display for nodes with comment status set to Hidden');
    $this->assertNoText(t('1 comment'), 'Non-empty comment count does not display for nodes with comment status set to Hidden');
112 113
  }
}