Skip to content

Instantly share code, notes, and snippets.

@davidjguru
Last active February 27, 2022 00:20
Show Gist options
  • Save davidjguru/589ab794e974a15699ed6fea683783f1 to your computer and use it in GitHub Desktop.
Save davidjguru/589ab794e974a15699ed6fea683783f1 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
<?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;
/**
* Checks if an admin user can access to the configuration page.
*/
public function testHumansTxtAdminAccess() {
// Build initial paths.
$humanstxt_config = Url::fromRoute('humanstxt.admin_settings_form', [], ['absolute' => FALSE])->toString();
// Create user for testing.
$this->adminUser = $this->drupalCreateUser(['administer humans.txt']);
// Login for the former admin user.
$this->drupalLogin($this->adminUser);
// Access to the path of humanstxt config page.
// @see UiHelperTrait::drupalGet
$this->drupalGet($humanstxt_config);
// Check the response returned by Drupal.
$this->assertResponse(200);
}
/**
* Checks if a non-administrative user cannot access to the config page.
*/
public function testHumansTxtUserNoAccess() {
// Build initial path.
$humanstxt_config = Url::fromRoute('humanstxt.admin_settings_form', [], ['absolute' => FALSE])->toString();
// Create user for testing.
$this->normalUser = $this->drupalCreateUser(['access content']);
// Login for the former basic user.
$this->drupalLogin($this->normalUser);
// Try access to the path of humanstxt config page.
// @see UiHelperTrait::drupalGet
$this->drupalGet($humanstxt_config);
// Check the response returned by Drupal.
$this->assertResponse(403);
// Logout as normal user and repeat the former cycle.
$this->drupalLogout();
$this->drupalGet($humanstxt_config);
$this->assertResponse(403);
}
/**
* Checks if an administrator can see the fields.
*/
public function testHumansTxtAdminFields() {
// Build initial path.
$humanstxt_config = Url::fromRoute('humanstxt.admin_settings_form', [], ['absolute' => FALSE])->toString();
// Create user for testing.
$this->adminUser = $this->drupalCreateUser(['administer humans.txt']);
// Login for the the former admin user.
$this->drupalLogin($this->adminUser);
// Access to the path of humanstxt config page.
// @see UiHelperTrait::drupalGet
$this->drupalGet($humanstxt_config);
// The textarea for configuring humans.txt is shown.
$this->assertSession()->fieldExists('humanstxt_content');
// The checkbox for configuring the creation of the humanstxt link is shown.
$this->assertSession()->fieldExists('humanstxt_display_link');
}
/**
* Checks if a non-administrative user cannot use the configuration page.
*/
public function testHumansTxtUserFields() {
// Build initial path.
$humanstxt_config = Url::fromRoute('humanstxt.admin_settings_form', [], ['absolute' => FALSE])->toString();
// Create user for testing.
$this->normalUser = $this->drupalCreateUser(['access content']);
// Login for the former basic user.
$this->drupalLogin($this->normalUser);
// Access to the path of humanstxt config page.
// @see UiHelperTrait::drupalGet
$this->drupalGet($humanstxt_config);
// The textarea is not shown for basic users.
$this->assertNoFieldById('edit-humanstxt-content', NULL);
// The checkbox is not shown for basic users.
$this->assertNoFieldById('edit-humanstxt-display-link', NULL);
}
/**
* Checks if the header is right.
*/
public function testHumansTxtHeader() {
// Build initial path.
$humanstxt_path = Url::fromRoute('humanstxt.content', [], ['absolute' => FALSE])->toString();
// Access to the path of humans.txt object/file.
// @see UiHelperTrait::drupalGet
$this->drupalGet($humanstxt_path);
// Check the returned response.
$this->assertResponse(200);
// Check if the file was served as text/plain with charset=UTF-8.
$this->assertHeader('Content-Type', 'text/plain; charset=UTF-8');
}
/**
* Checks if cache tags exists.
*/
public function testHumansTxtCacheTags() {
// Build initial path.
$humanstxt_path = Url::fromRoute('humanstxt.content', [], ['absolute' => FALSE])->toString();
// Access to the path of humans.txt object/file.
// @see UiHelperTrait::drupalGet
$this->drupalGet($humanstxt_path);
// Check the returned response.
$this->assertResponse(200);
// Check the related cache tag.
$this->assertCacheTag('humanstxt');
}
/**
* Checks if humans.txt file is delivered for Different Users with no link.
*/
public function testHumansTxtConfigureHumansTxtDifferentUsersNoLink() {
// Build initial paths.
$humanstxt_config = Url::fromRoute('humanstxt.admin_settings_form', [], ['absolute' => FALSE])->toString();
$humanstxt_path = Url::fromRoute('humanstxt.content', [], ['absolute' => TRUE])->toString();
$humanstxt_link = '<link rel="author" type="text/plain" hreflang="x-default" href="' . $humanstxt_path . '">';
// Create users for testing.
$this->adminUser = $this->drupalCreateUser(['administer humans.txt']);
$this->normalUser = $this->drupalCreateUser(['access content']);
// Login as admin and get the config form page.
$this->drupalLogin($this->adminUser);
$this->drupalGet($humanstxt_config);
// Load a new configuration for Humans.txt file and submit config Form.
$test_string = "# Testing Humans.txt {$this->randomMachineName()}";
$this->submitForm(['humanstxt_content' => $test_string, 'humanstxt_display_link' => FALSE], t('Save configuration'));
// Check the object/file created.
$this->drupalGet($humanstxt_path);
$this->assertResponse(200);
// Test header.
$this->assertHeader('Content-Type', 'text/plain; charset=UTF-8');
// Get page content.
$content = $this->getSession()->getPage()->getContent();
// Test the assert- if exists the test_string in the content.
$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 for Admins.
$this->drupalGet($humanstxt_config);
$this->assertResponse(200);
// All the HTML code is too much we just need to inspect the <head> section.
$tags = substr($this->getSession()->getPage()->getHtml(), 0, 2020);
$this->assertStringNotContainsString($humanstxt_link, $tags, sprintf('Test link: [%s] is shown in the -head- section from [%s] and and this shouldn\'t happen.', $humanstxt_link, $tags));
// Logout as admin and login as normal user.
$this->drupalLogout();
$this->drupalLogin($this->normalUser);
// Repeat the previous cycle now as normal user.
$this->drupalGet($humanstxt_path);
$this->assertResponse(200);
$this->assertHeader('Content-Type', 'text/plain; charset=UTF-8');
$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));
$this->drupalGet($humanstxt_config);
$this->assertResponse(403);
// All the HTML code is too much we just need to inspect the <head> section.
$tags = substr($this->getSession()->getPage()->getHtml(), 0, 2020);
$this->assertStringNotContainsString($humanstxt_link, $tags, sprintf('Test link: [%s] is shown in the -head- section from [%s] and this shouldn\'t happen.', $humanstxt_link, $tags));
// Logout as normal user.
$this->drupalLogout();
// Now a third iteration as anonymous user.
$this->drupalGet($humanstxt_path);
$this->assertResponse(200);
$this->assertHeader('Content-Type', 'text/plain; charset=UTF-8');
$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));
$this->drupalGet($humanstxt_config);
$this->assertResponse(403);
// All the HTML code is too much we just need to inspect the <head> section.
$tags = substr($this->getSession()->getPage()->getHtml(), 0, 2020);
$this->assertStringNotContainsString($humanstxt_link, $tags, sprintf('Test link: [%s] is shown in the -head- section from [%s] and this shouldn\'t happen.', $humanstxt_link, $tags));
}
/**
* Checks if humans.txt file is delivered for Different Users with a link.
*/
public function testHumansTxtConfigureHumansTxtDifferentUsersWithLink() {
// Build initial paths.
$humanstxt_config = Url::fromRoute('humanstxt.admin_settings_form', [], ['absolute' => FALSE])->toString();
$humanstxt_path = Url::fromRoute('humanstxt.content', [], ['absolute' => TRUE])->toString();
$humanstxt_link = '<link rel="author" type="text/plain" hreflang="x-default" href="' . $humanstxt_path . '">';
// Create users for testing.
$this->adminUser = $this->drupalCreateUser(['administer humans.txt']);
$this->normalUser = $this->drupalCreateUser(['access content']);
// Login as admin and get the config form page.
$this->drupalLogin($this->adminUser);
$this->drupalGet($humanstxt_config);
// Load a new configuration for Humans.txt file and submit config Form.
$test_string = "# Testing Humans.txt {$this->randomMachineName()}";
$this->submitForm(['humanstxt_content' => $test_string, 'humanstxt_display_link' => TRUE], t('Save configuration'));
// Check the object/file created.
$this->drupalGet($humanstxt_path);
$this->assertResponse(200);
// Test header.
$this->assertHeader('Content-Type', 'text/plain; charset=UTF-8');
// Get page content.
$content = $this->getSession()->getPage()->getContent();
// Test the assert- if exists the test_string in the content.
$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 for Admins.
$this->drupalGet($humanstxt_config);
$this->assertResponse(200);
// All the HTML code is too much we just need to inspect the <head> section.
$tags = substr($this->getSession()->getPage()->getHtml(), 0, 2020);
$this->assertStringContainsString($humanstxt_link, $tags, sprintf('Test link: [%s] is NOT shown in the -head- section from [%s] and this shouldn\'t happen.', $humanstxt_link, $tags));
// Logout as admin and login as normal user.
$this->drupalLogout();
$this->drupalLogin($this->normalUser);
// Repeat the previous cycle now as normal user.
$this->drupalGet($humanstxt_path);
$this->assertResponse(200);
$this->assertHeader('Content-Type', 'text/plain; charset=UTF-8');
$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));
$this->drupalGet($humanstxt_config);
$this->assertResponse(403);
// All the HTML code is too much we just need to inspect the <head> section.
$tags = substr($this->getSession()->getPage()->getHtml(), 0, 2020);
$this->assertStringContainsString($humanstxt_link, $tags, sprintf('Test link: [%s] is NOT shown in the -head- section from [%s] and this shouldn\'t happen.', $humanstxt_link, $tags));
// Logout as normal user.
$this->drupalLogout();
// Now a third iteration as anonymous user.
$this->drupalGet($humanstxt_path);
$this->assertResponse(200);
$this->assertHeader('Content-Type', 'text/plain; charset=UTF-8');
$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));
$this->drupalGet($humanstxt_config);
$this->assertResponse(403);
// All the HTML code is too much we just need to inspect the <head> section.
$tags = substr($this->getSession()->getPage()->getHtml(), 0, 2020);
$this->assertStringContainsString($humanstxt_link, $tags, sprintf('Test link: [%s] is NOT shown in the -head- section from [%s] and this shouldn\'t happen.', $humanstxt_link, $tags));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment