RobotsTxtBasicTest.php in RobotsTxt 8
File
tests/src/Functional/RobotsTxtBasicTest.php
View source
<?php
namespace Drupal\Tests\robotstxt\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class RobotsTxtBasicTest extends BrowserTestBase {
use StringTranslationTrait;
protected $defaultTheme = 'stark';
protected static $modules = [
'robotstxt',
'node',
'robotstxt_test',
];
protected $adminUser;
protected $normalUser;
public function testRobotsTxtAdminAccess() {
$this->adminUser = $this
->drupalCreateUser([
'administer robots.txt',
]);
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/config/search/robotstxt');
$this
->assertSession()
->fieldExists('robotstxt_content');
}
public function testRobotsTxtUserNoAccess() {
$this->normalUser = $this
->drupalCreateUser([
'access content',
]);
$this
->drupalLogin($this->normalUser);
$this
->drupalGet('admin/config/search/robotstxt');
$this
->assertSession()
->statusCodeEquals(403);
$this
->assertSession()
->fieldNotExists('edit-robotstxt-content');
}
public function testRobotsTxtPath() {
$this
->drupalGet('robots-test.txt');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->responseHeaderEquals('Content-Type', 'text/plain; charset=UTF-8');
}
public function testRobotsTxtCacheTags() {
$this
->drupalGet('robots-test.txt');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->responseHeaderContains('X-Drupal-Cache-Tags', 'robotstxt');
}
public function testRobotsTxtConfigureRobotsTxt() {
$this->adminUser = $this
->drupalCreateUser([
'administer robots.txt',
]);
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/config/search/robotstxt');
$test_string = "# SimpleTest {$this->randomMachineName()}";
$this
->submitForm([
'robotstxt_content' => $test_string,
], $this
->t('Save configuration'));
$this
->drupalLogout();
$this
->drupalGet('robots-test.txt');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->responseHeaderEquals('Content-Type', 'text/plain; charset=UTF-8');
$content = $this
->getSession()
->getPage()
->getContent();
$this
->assertTrue($content == $test_string, sprintf('Test string [%s] is displayed in the configured robots.txt file [%s].', $test_string, $content));
}
}