headless_cms-1.0.3/modules/headless_cms_preview/tests/src/Functional/PreviewTest.php
modules/headless_cms_preview/tests/src/Functional/PreviewTest.php
<?php
declare(strict_types=1);
namespace Drupal\Tests\headless_cms_preview\Functional;
use Drupal\Core\Session\AccountInterface;
use Drupal\Tests\BrowserTestBase;
use Drupal\user\Entity\User;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
/**
* Tests headless preview functionality.
*/
class PreviewTest extends BrowserTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = [
'consumers',
'jsonapi',
'node',
'headless_cms',
'headless_cms_preview',
'system',
'serialization',
'user',
];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* The admin user.
*/
protected AccountInterface $admin;
/**
* The http kernel.
*/
protected HttpKernelInterface $httpKernel;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
$this->drupalCreateContentType([
'type' => 'page',
'name' => 'Page',
]);
// Add the article bundle to enabled_bundles.
$this->config('headless_cms_preview.settings')
->set('enabled_bundles', ['page'])
->save();
$this->httpKernel = $this->container->get('http_kernel');
$this->admin = $this->drupalCreateUser([], NULL, TRUE);
// Needed for jsonapi to pick up newly created content types.
$this->rebuildAll();
}
/**
* Test preview functionality on create form.
*/
public function testPreviewActionOnCreateForm() {
$this->drupalLogin($this->admin);
$this->assertSession()->pageTextContains('Member for');
$this->drupalGet('/node/add/article');
$this->assertSession()->pageTextContains('Create Article');
$this->assertSession()->elementNotExists('css', '#edit-save-preview');
$this->drupalGet('/node/add/page');
$this->assertSession()->pageTextContains('Create Page');
$this->assertSession()->elementExists('css', '#edit-save-preview');
$this->assertSession()->pageTextNotContains('Preview Token:');
$edit = [
'title[0][value]' => 'Test article',
];
$this->submitForm($edit, 'Save Preview');
$this->assertSession()->pageTextContains('Preview Token:');
}
/**
* Test preview functionality on edit form.
*/
public function testPreviewActionOnEditForm() {
$this->drupalLogin($this->admin);
$this->assertSession()->pageTextContains('Member for');
$this->drupalGet('/node/add/page');
$this->assertSession()->pageTextContains('Create Page');
$edit = [
'title[0][value]' => 'Test page',
];
$this->submitForm($edit, 'Save');
$this->drupalGet('/node/1/edit');
$this->assertSession()->pageTextContains('Test Page');
$this->assertSession()->elementExists('css', '#edit-save-preview');
$this->assertSession()->pageTextNotContains('Preview Token:');
$this->submitForm($edit, 'Save Preview');
$this->assertSession()->pageTextContains('Preview Token:');
}
/**
* Test the JSON:API node create preview.
*/
public function testJsonApiNodeCreatePreview() {
$this->drupalLogin($this->admin);
$this->assertSession()->pageTextContains('Member for');
$this->drupalGet('/node/add/page');
$this->assertSession()->pageTextContains('Create Page');
$edit = [
'title[0][value]' => 'Test page',
];
$this->submitForm($edit, 'Save Preview');
$uuid = explode('=', $this->getUrl())[1];
$previewToken = $this->getSession()->getPage()->find('css', '#preview-token-value')->getValue();
$uri = sprintf('/jsonapi/node/page/%s/preview', $uuid);
$jsonapiResponse = $this->makeJsonApiRequest($uri, [
'X-Headless-Preview-Token' => $previewToken,
]);
$this->assertStringContainsString('Test page', $jsonapiResponse);
$edit = [
'title[0][value]' => 'Test Update 1',
];
$this->submitForm($edit, 'Save Preview');
$jsonapiResponse = $this->makeJsonApiRequest($uri, [
'X-Headless-Preview-Token' => $previewToken,
]);
$previewToken = $this->getSession()->getPage()->find('css', '#preview-token-value')->getValue();
$this->assertStringContainsString('Test Update 1', $jsonapiResponse);
}
/**
* Test the JSON:API node update preview.
*/
public function testJsonApiNodeUpdatePreview() {
$this->drupalLogin($this->admin);
$this->assertSession()->pageTextContains('Member for');
$this->drupalGet('/node/add/page');
$this->assertSession()->pageTextContains('Create Page');
$edit = [
'title[0][value]' => 'Test page',
];
$this->submitForm($edit, 'Save');
$this->drupalGet('/node/1/edit');
$this->assertSession()->pageTextContains('Test page');
// Update exising node in form.
$edit = [
'title[0][value]' => 'Test Update 1',
];
$this->submitForm($edit, 'Save Preview');
$uuid = explode('=', $this->getUrl())[1];
$previewToken = $this->getSession()->getPage()->find('css', '#preview-token-value')->getValue();
$uri = sprintf('/jsonapi/node/page/%s/preview', $uuid);
$jsonapiResponse = $this->makeJsonApiRequest($uri, [
'X-Headless-Preview-Token' => $previewToken,
]);
$this->assertStringContainsString('Test Update 1', $jsonapiResponse);
// Update second time without saving.
$edit = [
'title[0][value]' => 'Test Update 2',
];
$this->submitForm($edit, 'Save Preview');
$jsonapiResponse = $this->makeJsonApiRequest($uri, [
'X-Headless-Preview-Token' => $previewToken,
]);
$previewToken = $this->getSession()->getPage()->find('css', '#preview-token-value')->getValue();
$this->assertStringContainsString('Test Update 2', $jsonapiResponse);
}
/**
* Test the JSON:API preview without token.
*/
public function testJsonApiPreviewWithoutToken() {
$this->drupalLogin($this->admin);
$this->assertSession()->pageTextContains('Member for');
$this->drupalGet('/node/add/page');
$this->assertSession()->pageTextContains('Create Page');
$edit = [
'title[0][value]' => 'Test page',
];
$this->submitForm($edit, 'Save Preview');
$uuid = explode('=', $this->getUrl())[1];
$uri = sprintf('/jsonapi/node/page/%s/preview', $uuid);
$jsonapiResponse = $this->makeJsonApiRequest($uri);
$this->assertStringContainsString('Not Found', $jsonapiResponse);
}
/**
* Make a GET JSON:API request.
*/
protected function makeJsonApiRequest(string $uri, array $headers = [], array $query = []): string {
$this->setCurrentUser(User::getAnonymousUser());
$request = Request::create($uri, 'GET', $query);
$request->headers->set('Content-Type', 'application/vnd.api+json');
$request->headers->add($headers);
$res = $this->httpKernel->handle($request)->getContent();
$this->setCurrentUser($this->admin);
return $res;
}
}
