Skip to content

Instantly share code, notes, and snippets.

@davidjguru
Created April 13, 2020 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidjguru/58f64fc0e1335e734ce4d1b387d913a0 to your computer and use it in GitHub Desktop.
Save davidjguru/58f64fc0e1335e734ce4d1b387d913a0 to your computer and use it in GitHub Desktop.
Functional Testing in Drupal 8|9: Humans.txt Basic Test Class based in BrowserTestBase / PHPUnit / Mink (Second version)
<?php
namespace Drupal\Tests\humanstxt\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\Core\Url;
/**
* Tests basic functionality of configured humans.txt files.
*
* @group Humans.txt
*/
class HumansTxtBasicTest extends BrowserTestBase {
/**
* Provides the default theme.
*
* @var string
*/
protected $defaultTheme = 'stark';
/**
* Modules to enable.
*
* @var array
*/
public static $modules = ['humanstxt', 'user'];
/**
* User with proper permissions for module configuration.
*
* @var \Drupal\user\Entity\User|false
*/
protected $adminUser;
/**
* User with content access.
*
* @var \Drupal\user\Entity\User|false
*/
protected $normalUser;
/**
* HTML Link to a Humans.txt file.
*
* @var string
*/
protected $fileLink;
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
// Create users.
$this->adminUser = $this->drupalCreateUser(['administer humans.txt']);
$this->normalUser = $this->drupalCreateUser(['access content']);
// Create link.
$this->fileLink = '<link rel="author" type="text/plain" hreflang="x-default" href="' .
Url::fromRoute('humanstxt.content', [], ['absolute' => TRUE])->toString() . '">';
}
/**
* Checks if a non-administrative user cannot access to the config page.
*/
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);
}
/**
* Checks if the header is right.
*/
public function testHumansTxtHeader() {
$this->drupalGet('humans.txt');
$this->assertResponse(200);
$this->assertHeader('Content-Type', 'text/plain; charset=UTF-8');
}
/**
* Checks if cache tags exists.
*/
public function testHumansTxtCacheTags() {
$this->drupalGet('humans.txt');
$this->assertResponse(200);
$this->assertCacheTag('humanstxt');
}
/**
* Checks if humans.txt file is delivered with no link.
*/
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();
// Test the newly created humans.txt file.
$this->drupalGet('humans.txt');
$this->assertResponse(200);
$content = $this->getSession()->getPage()->getContent();
$this->assertTrue($content == $test_string, sprintf('Test string: [%s] is NOT equals in the humans.txt file [%s] and this shouldn\'t happen.', $test_string, $content));
// Test if the link to the object/file is not in HTML <head> section.
$this->drupalGet('/admin/config/development/humanstxt');
$tags = substr($this->getSession()->getPage()->getHtml(), 0, 2020);
$this->assertStringNotContainsString($this->fileLink, $tags, sprintf('Test link: [%s] is shown in the -head- section from [%s] and and this shouldn\'t happen.', $this->fileLink, $tags));
}
/**
* Checks if humans.txt file is delivered for with a link.
*/
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();
// Test the newly created humans.txt file.
$this->drupalGet('humans.txt');
$this->assertResponse(200);
$content = $this->getSession()->getPage()->getContent();
$this->assertTrue($content == $test_string, sprintf('Test string: [%s] is NOT equals in the humans.txt file [%s] and this shouldn\'t happen.', $test_string, $content));
// Test if the link to the object/file is in HTML <head> section.
$this->drupalGet('/admin/config/development/humanstxt');
$tags = substr($this->getSession()->getPage()->getHtml(), 0, 2020);
$this->assertStringContainsString($this->fileLink, $tags, sprintf('Test link: [%s] is NOT shown in the -head- section from [%s] and this shouldn\'t happen.', $this->fileLink, $tags));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment