View source
<?php
namespace Drupal\Tests\search\Functional;
use Drupal\Tests\BrowserTestBase;
class SearchEmbedFormTest extends BrowserTestBase {
protected static $modules = [
'node',
'search',
'search_embedded_form',
];
protected $defaultTheme = 'stark';
protected $node;
protected $submitCount = 0;
protected function setUp() {
parent::setUp();
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Basic page',
]);
$test_user = $this
->drupalCreateUser([
'access content',
'search content',
'administer nodes',
]);
$this
->drupalLogin($test_user);
$this->node = $this
->drupalCreateNode();
$this->container
->get('plugin.manager.search')
->createInstance('node_search')
->updateIndex();
$this->submitCount = \Drupal::state()
->get('search_embedded_form.submit_count');
$this
->refreshVariables();
}
public function testEmbeddedForm() {
$this
->drupalPostForm('search_embedded_form', [
'name' => 'John',
], t('Send away'));
$this
->assertText(t('Test form was submitted'), 'Form message appears');
$count = \Drupal::state()
->get('search_embedded_form.submit_count');
$this
->assertEqual($this->submitCount + 1, $count, 'Form submission count is correct');
$this->submitCount = $count;
$this
->drupalGet('search/node', [
'query' => [
'keys' => $this->node
->label(),
],
]);
$this
->assertText(t('Your name'), 'Form is visible');
$this
->drupalPostForm(NULL, [
'name' => 'John',
], t('Send away'));
$this
->assertText(t('Test form was submitted'), 'Form message appears');
$count = \Drupal::state()
->get('search_embedded_form.submit_count');
$this
->assertEqual($this->submitCount + 1, $count, 'Form submission count is correct');
$this->submitCount = $count;
$this
->drupalPostForm('search', [
'keys' => 'foo',
], t('Search'));
$this
->assertNoText(t('Test form was submitted'), 'Form message does not appear');
$count = \Drupal::state()
->get('search_embedded_form.submit_count');
$this
->assertEqual($this->submitCount, $count, 'Form submission count is correct');
$this->submitCount = $count;
}
}