View source
<?php
namespace Drupal\Tests\apigee_api_catalog\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Url;
use Drupal\file\Entity\File;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\TestFileCreationTrait;
class ApiDocsAdminTest extends BrowserTestBase {
use TestFileCreationTrait;
public static $modules = [
'apigee_api_catalog',
'block',
'field_ui',
];
protected $adminUser;
protected function setupMenus() {
$this
->drupalPlaceBlock('local_actions_block', [
'region' => 'content',
]);
}
protected function setUp() {
parent::setUp();
$this
->setupMenus();
$this->adminUser = $this
->drupalCreateUser([
'add apidoc entities',
'delete apidoc entities',
'edit apidoc entities',
'view published apidoc entities',
'view unpublished apidoc entities',
'administer apigee api catalog',
'administer apidoc display',
'administer apidoc fields',
'administer apidoc form display',
'access administration pages',
'access content',
]);
$this
->drupalLogin($this->adminUser);
}
public function testApiDocAdministration() {
$header_selector = 'table .empty';
$assert = $this
->assertSession();
$this
->drupalGet(Url::fromRoute('entity.apidoc.collection'));
$assert
->elementTextContains('css', $header_selector, 'There are no API docs yet.');
$assert
->linkExists('Add API Doc');
$this
->clickLink('Add API Doc');
$assert
->fieldValueEquals('name[0][value]', '');
$assert
->fieldValueEquals('description[0][value]', '');
$assert
->fieldValueEquals('status[value]', '1');
$file = File::create([
'uid' => $this->adminUser
->id(),
'filename' => 'specA.yml',
'uri' => 'public://specA.yml',
'filemime' => 'application/octet-stream',
'created' => 1,
'changed' => 1,
'status' => FILE_STATUS_PERMANENT,
]);
file_put_contents($file
->getFileUri(), "swagger: '2.0'");
$file
->save();
$this
->assertTrue($file
->id() > 0, 'The file was added to the database.');
$page = $this
->getSession()
->getPage();
$random_name = $this
->randomMachineName();
$random_description = $this->randomGenerator
->sentences(5);
$page
->fillField('name[0][value]', $random_name);
$page
->fillField('description[0][value]', $random_description);
$this
->getSession()
->getPage()
->find('css', 'input[name="spec[0][fids]"]')
->setValue($file
->id());
$this
->getSession()
->getPage()
->pressButton(t('Save'));
$assert
->statusCodeEquals(200);
$assert
->pageTextContains(new FormattableMarkup('Created the @name API Doc.', [
'@name' => $random_name,
]));
$assert
->linkExists($random_name);
$assert
->linkExists('Edit');
$assert
->linkExists('Delete');
$this
->clickLink('Edit');
$assert
->statusCodeEquals(200);
$assert
->fieldValueEquals('name[0][value]', $random_name);
$assert
->fieldValueEquals('description[0][value]', $random_description);
$assert
->fieldValueEquals('status[value]', '1');
$assert
->linkExists('specA.yml');
$this
->clickLink('Delete');
$assert
->linkExists('Cancel');
$this
->drupalPostForm(NULL, [], 'Delete');
$assert
->pageTextNotContains($random_name);
}
}