SwaggerUiFieldFormatterTest.php in Swagger UI Field Formatter 8.3
File
tests/src/FunctionalJavascript/SwaggerUiFieldFormatterTest.php
View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\swagger_ui_formatter\FunctionalJavascript;
use Drupal\Core\Cache\Cache;
use Drupal\Core\StreamWrapper\PublicStream;
use Drupal\Core\Url;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
final class SwaggerUiFieldFormatterTest extends WebDriverTestBase {
protected static $modules = [
'swagger_ui_formatter_test',
];
protected $defaultTheme = 'stark';
private $fileSystem;
protected function setUp() : void {
parent::setUp();
$this->fileSystem = $this->container
->get('file_system');
$module_path = drupal_get_path('module', 'swagger_ui_formatter');
$this->fileSystem
->copy(DRUPAL_ROOT . '/' . $module_path . '/tests/fixtures/openapi20/petstore-expanded.yaml', PublicStream::basePath());
$this->fileSystem
->copy(DRUPAL_ROOT . '/' . $module_path . '/tests/fixtures/openapi30/uspto.yaml', PublicStream::basePath());
}
public function testFileFormatter() : void {
$page = $this
->getSession()
->getPage();
$assert = $this
->assertSession();
$this
->drupalLogin($this->rootUser);
$this
->drupalGet(Url::fromRoute('node.add', [
'node_type' => 'api_doc',
]));
$petstore_path = $this->fileSystem
->realpath('public://petstore-expanded.yaml');
$uspto_path = $this->fileSystem
->realpath('public://uspto.yaml');
$page
->attachFileToField('files[field_api_spec_0][]', $petstore_path);
$assert
->waitForField('field_api_spec_0_remove_button');
$page
->attachFileToField('files[field_api_spec_1][]', $uspto_path);
$assert
->waitForField('field_api_spec_1_remove_button');
$this
->createScreenshot(__FUNCTION__ . '-after-file-upload');
$this
->drupalPostForm(NULL, [
'title[0][value]' => 'Testing the file field formatter',
], 'Save');
$this
->createScreenshot(__FUNCTION__ . '-after-save');
$assert
->waitForField('swagger-ui-field_api_spec-0');
$assert
->pageTextContains('Swagger Petstore');
$assert
->waitForField('swagger-ui-field_api_spec-1');
$assert
->pageTextContains('USPTO Data Set API');
$this
->validateSwaggerUiErrorMessage(__FUNCTION__);
}
public function testLinkFormatter() : void {
$page = $this
->getSession()
->getPage();
$assert = $this
->assertSession();
$this
->drupalLogin($this->rootUser);
$this
->drupalGet(Url::fromRoute('node.add', [
'node_type' => 'remote_api_doc',
]));
$page
->fillField('field_remote_api_spec[0][uri]', file_create_url('public://petstore-expanded.yaml'));
$page
->pressButton('field_remote_api_spec_add_more');
$assert
->waitForField('field_remote_api_spec[1][uri]');
$page
->fillField('field_remote_api_spec[1][uri]', file_create_url('public://uspto.yaml'));
$this
->drupalPostForm(NULL, [
'title[0][value]' => 'Testing the link field formatter',
], 'Save');
$this
->createScreenshot(__FUNCTION__ . '-after-save');
$assert
->waitForField('swagger-ui-field_api_spec-0');
$assert
->pageTextContains('Swagger Petstore');
$assert
->waitForField('swagger-ui-field_api_spec-1');
$assert
->pageTextContains('USPTO Data Set API');
$this
->validateSwaggerUiErrorMessage(__FUNCTION__);
}
protected function createScreenshot($filename, $set_background_color = TRUE) : void {
parent::createScreenshot(DRUPAL_ROOT . '/sites/simpletest/' . $filename . '.png', $set_background_color);
}
private function validateSwaggerUiErrorMessage(string $filename_prefix) : void {
$assert = $this
->assertSession();
$swagger_ui_library_error_msg = 'The Swagger UI library is missing, incorrectly defined or not supported.';
$swagger_ui_library_discovery = \Drupal::service('swagger_ui_formatter.swagger_ui_library_discovery');
$swagger_ui_library_discovery
->fakeMissingLibrary(TRUE);
$this
->getSession()
->reload();
$this
->createScreenshot($filename_prefix . '-' . __FUNCTION__ . '-after-fake-missing-library-enabled');
$assert
->pageTextContainsOnce($swagger_ui_library_error_msg);
$swagger_ui_library_discovery
->fakeMissingLibrary(FALSE);
Cache::invalidateTags([
'rendered',
]);
$this
->getSession()
->reload();
$this
->createScreenshot($filename_prefix . '-' . __FUNCTION__ . '-after-fake-missing-library-disabled');
$assert
->pageTextNotContains($swagger_ui_library_error_msg);
$swagger_ui_library_discovery
->fakeUnsupportedLibrary(TRUE);
$this
->getSession()
->reload();
$this
->createScreenshot($filename_prefix . '-' . __FUNCTION__ . '-after-fake-unsupported-library-enabled');
$assert
->pageTextContainsOnce($swagger_ui_library_error_msg);
$swagger_ui_library_discovery
->fakeUnsupportedLibrary(FALSE);
Cache::invalidateTags([
'rendered',
]);
$this
->getSession()
->reload();
$this
->createScreenshot($filename_prefix . '-' . __FUNCTION__ . '-after-fake-unsupported-library-disabled');
$assert
->pageTextNotContains($swagger_ui_library_error_msg);
}
}