View source
<?php
namespace Drupal\search\Tests;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\comment\Tests\CommentTestTrait;
class SearchCommentCountToggleTest extends SearchTestBase {
use CommentTestTrait;
public static $modules = array(
'node',
'comment',
);
protected $searchingUser;
protected $searchableNodes;
protected function setUp() {
parent::setUp();
$this->searchingUser = $this
->drupalCreateUser(array(
'search content',
'access content',
'access comments',
'post comments',
'skip comment approval',
));
$this
->drupalLogin($this->searchingUser);
$this
->addDefaultCommentField('node', 'article');
$node_params = array(
'type' => 'article',
'body' => array(
array(
'value' => 'SearchCommentToggleTestCase',
),
),
);
$this->searchableNodes['1 comment'] = $this
->drupalCreateNode($node_params);
$this->searchableNodes['0 comments'] = $this
->drupalCreateNode($node_params);
$edit_comment = array();
$edit_comment['subject[0][value]'] = $this
->randomMachineName();
$edit_comment['comment_body[0][value]'] = $this
->randomMachineName();
$this
->drupalPostForm('comment/reply/node/' . $this->searchableNodes['1 comment']
->id() . '/comment', $edit_comment, t('Save'));
$this->container
->get('plugin.manager.search')
->createInstance('node_search')
->updateIndex();
search_update_totals();
}
function testSearchCommentCountToggle() {
$edit = array(
'keys' => "'SearchCommentToggleTestCase'",
);
$this
->drupalGet('search/node');
$this
->drupalPostForm(NULL, $edit, t('Search'));
$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');
$this->searchableNodes['0 comments']
->set('comment', CommentItemInterface::CLOSED);
$this->searchableNodes['0 comments']
->save();
$this->searchableNodes['1 comment']
->set('comment', CommentItemInterface::CLOSED);
$this->searchableNodes['1 comment']
->save();
$this
->drupalPostForm(NULL, $edit, t('Search'));
$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');
$this->searchableNodes['0 comments']
->set('comment', CommentItemInterface::HIDDEN);
$this->searchableNodes['0 comments']
->save();
$this->searchableNodes['1 comment']
->set('comment', CommentItemInterface::HIDDEN);
$this->searchableNodes['1 comment']
->save();
$this
->drupalPostForm(NULL, $edit, t('Search'));
$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');
}
}