PageSpecificTest.php in Page Specific Class 2.0.x
File
test/src/Functional/PageSpecificTest.php
View source
<?php
namespace Drupal\Tests\page_specific_class\Functional;
use Drupal\Core\Url;
use Drupal\node\Entity\Node;
use Drupal\Tests\BrowserTestBase;
class PageSpecificTest extends BrowserTestBase {
protected $defaultTheme = 'seven';
public static $modules = [
'page_specific_class',
'node',
];
protected $profile = 'standard';
protected function setUp() : void {
parent::setUp();
$admin_user = $this
->drupalCreateUser([
'administer site configuration',
'access administration pages',
]);
$this
->drupalLogin($admin_user);
}
public function testFunctional() {
$this
->doTestRoutes();
$this
->doTestForm();
}
public function doTestRoutes() {
$assertion = $this
->assertSession();
$path = Url::fromRoute("page_specific_class.settings");
$this
->drupalGet($path);
$assertion
->statusCodeEquals(200);
}
public function doTestForm() {
$assertion = $this
->assertSession();
$node = Node::create([
'type' => 'page',
'title' => 'Node Example',
]);
$node
->save();
$nid = $node
->id();
$path = Url::fromRoute("page_specific_class.settings");
$this
->drupalGet($path);
$edit = [
'url_with_class' => "/node/{$nid}|custom-node",
];
$this
->drupalPostForm(Url::fromRoute('page_specific_class.settings'), $edit, 'Save configuration');
$assertion
->pageTextContains('The configuration options have been saved.');
$this
->drupalGet("/node/{$nid}");
$body_class_element = $this
->xpath("//body[contains(@class, 'custom-node')]");
$this
->assertTrue(!empty($body_class_element), 'custom-node class not found.');
}
}