HumansTxtBasicTest.php in Humans.txt 2.x
File
tests/src/Functional/HumansTxtBasicTest.php
View source
<?php
namespace Drupal\Tests\humanstxt\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\Core\Url;
class HumansTxtBasicTest extends BrowserTestBase {
protected $defaultTheme = 'stark';
protected static $modules = [
'humanstxt',
'user',
];
protected $adminUser;
protected $normalUser;
protected $fileLink;
public function setUp() {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser([
'administer humans.txt',
]);
$this->normalUser = $this
->drupalCreateUser([
'access content',
]);
$this->fileLink = '<link rel="author" type="text/plain" hreflang="x-default" href="' . Url::fromRoute('humanstxt.content', [], [
'absolute' => TRUE,
])
->toString() . '">';
}
public function testHumansTxtUserNoAccess() {
$this
->drupalGet('/admin/config/development/humanstxt');
$this
->assertResponse(403);
$this
->drupalLogin($this->normalUser);
$this
->drupalGet('/admin/config/development/humanstxt');
$this
->assertResponse(403);
}
public function testHumansTxtHeader() {
$this
->drupalGet('humans.txt');
$this
->assertResponse(200);
$this
->assertHeader('Content-Type', 'text/plain; charset=UTF-8');
}
public function testHumansTxtCacheTags() {
$this
->drupalGet('humans.txt');
$this
->assertResponse(200);
$this
->assertCacheTag('humanstxt');
}
public function testHumansTxtConfigureHumansTxtNoLink() {
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('/admin/config/development/humanstxt');
$this
->assertResponse(200);
$test_string = "# Testing Humans.txt {$this->randomMachineName()}";
$this
->submitForm([
'humanstxt_content' => $test_string,
'humanstxt_display_link' => FALSE,
], t('Save configuration'));
$this
->drupalLogout();
$this
->drupalGet('humans.txt');
$this
->assertResponse(200);
$content = $this
->getSession()
->getPage()
->getContent();
$this
->assertTrue($content == $test_string, sprintf('Test string: [%s] is displayed in the configured humans.txt file.', $test_string));
$this
->drupalGet('/admin/config/development/humanstxt');
$tags = $this
->getSession()
->getPage()
->getHtml();
$this
->assertNotContains($this->fileLink, $tags, sprintf('Humans.txt link: [%s] is not shown in the -head- section.', $this->fileLink));
}
public function testHumansTxtConfigureHumansTxtWithLink() {
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('/admin/config/development/humanstxt');
$this
->assertResponse(200);
$test_string = "# Testing Humans.txt {$this->randomMachineName()}";
$this
->submitForm([
'humanstxt_content' => $test_string,
'humanstxt_display_link' => TRUE,
], t('Save configuration'));
$this
->drupalLogout();
$this
->drupalGet('humans.txt');
$this
->assertResponse(200);
$content = $this
->getSession()
->getPage()
->getContent();
$this
->assertTrue($content == $test_string, sprintf('Test string: [%s] is displayed in the configured humans.txt file.', $test_string));
$this
->drupalGet('/admin/config/development/humanstxt');
$tags = $this
->getSession()
->getPage()
->getHtml();
$this
->assertContains($this->fileLink, $tags, sprintf('Humans.txt link: [%s] is shown in the -head- section.', $this->fileLink));
}
}