View source
<?php
namespace Drupal\Tests\node_type_example\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Tests\examples\Functional\ExamplesBrowserTestBase;
use Drupal\node\Entity\NodeType;
use Drupal\node\NodeInterface;
use Drupal\node\NodeTypeInterface;
class NodeTypeExampleTest extends ExamplesBrowserTestBase {
protected $defaultTheme = 'stark';
public static $modules = [
'node',
'node_type_example',
];
protected $profile = 'minimal';
protected function providerMenuLinks() {
return [
'' => '/examples/node-type-example',
];
}
public function testNodeTypeExample() {
$assert = $this
->assertSession();
$this
->drupalGet('/examples/node-type-example');
$assert
->statusCodeEquals(200);
$links = $this
->providerMenuLinks();
foreach ($links as $page => $path) {
$this
->drupalGet($page);
$assert
->linkByHrefExists($path);
}
}
public function testNodeTypes() {
$assert = $this
->assertSession();
$admin_user = $this
->drupalCreateUser([
'administer content types',
]);
$this
->drupalLogin($admin_user);
$this
->drupalGet('/admin/structure/types');
$assert
->pageTextContains('Example: Basic Content Type', 'Basic content type found.');
$assert
->pageTextContains('Example: Locked Content Type', 'Locked content type found.');
$node_type = NodeType::load('basic_content_type');
$this
->assertInstanceOf(NodeTypeInterface::class, $node_type, 'basic_content_type exists.');
if ($node_type) {
$this
->assertFalse($node_type
->isLocked(), 'basic_content_type is not locked.');
}
$node_type = NodeType::load('locked_content_type');
$this
->assertInstanceOf(NodeTypeInterface::class, $node_type, 'locked_content_type exists.');
if ($node_type) {
$this
->assertEquals('locked_content_type', $node_type
->isLocked());
}
$creator_user = $this
->drupalCreateUser([
'create basic_content_type content',
]);
$this
->drupalLogin($creator_user);
$edit = [];
$edit['title[0][value]'] = $this
->randomMachineName(8);
$edit['body[0][value]'] = $this
->randomMachineName(16);
$this
->drupalPostForm('/node/add/basic_content_type', $edit, 'Save');
$assert
->pageTextContains((string) new FormattableMarkup('@post @title has been created.', [
'@post' => 'Example: Basic Content Type',
'@title' => $edit['title[0][value]'],
]));
$node = $this
->drupalGetNodeByTitle($edit['title[0][value]']);
$this
->assertInstanceOf(NodeInterface::class, $node, 'Node found in database.');
}
public function testNodeCreation() {
$this
->drupalLogin($this
->drupalCreateUser([
'create basic_content_type content',
'create locked_content_type content',
]));
$title = 'Test title.';
$body = 'Test body.';
$edit = [];
$edit['title[0][value]'] = $title;
$edit['body[0][value]'] = $body;
$this
->drupalPostForm('/node/add/basic_content_type', $edit, 'Save');
$this
->assertText($title);
$this
->assertText($body);
$this
->drupalPostForm('/node/add/locked_content_type', $edit, 'Save');
$this
->assertText($title);
$this
->assertText($body);
}
public function testUninstallReinstall() {
$session = $this
->assertSession();
$module_installer = $this->container
->get('module_installer');
$module_installer
->uninstall([
'node_type_example',
]);
$this
->drupalGet('examples/node-type-example');
$session
->statusCodeEquals(404);
$module_installer
->install([
'node_type_example',
]);
$this
->drupalGet('examples/node-type-example');
$session
->statusCodeEquals(200);
}
}