You are here

public function RobotsTxtBasicTest::testRobotsTxtConfigureRobotsTxt in RobotsTxt 8

Checks that a configured robots.txt file is delivered as configured.

File

tests/src/Functional/RobotsTxtBasicTest.php, line 101

Class

RobotsTxtBasicTest
Tests basic functionality of configured robots.txt files.

Namespace

Drupal\Tests\robotstxt\Functional

Code

public function testRobotsTxtConfigureRobotsTxt() {

  // Create an admin user, log in and access settings form.
  $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');

  // No local robots.txt file was detected, and an anonymous user is delivered
  // content at the /robots.txt path.
  $this
    ->assertSession()
    ->statusCodeEquals(200);

  // The robots.txt file was served with header
  // Content-Type: "text/plain; charset=UTF-8".
  $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));
}