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;
protected $defaultTheme = 'stark';
public static $modules = [
'views',
'apigee_api_catalog',
'block',
'field_ui',
];
protected $adminUser;
protected function setupMenus() {
$this
->drupalPlaceBlock('local_actions_block');
$this
->drupalPlaceBlock('local_tasks_block');
}
protected function setUp() {
parent::setUp();
$this
->setupMenus();
$this->adminUser = $this
->drupalCreateUser([
'access content',
'access content overview',
'create apidoc content',
'edit any apidoc content',
'delete any apidoc content',
]);
$this
->drupalLogin($this->adminUser);
}
public function testApiDocAdministration() {
$assert = $this
->assertSession();
$this
->drupalGet(Url::fromRoute('view.api_catalog_admin.page_1'));
$assert
->pageTextContains('There are no API docs yet.');
$assert
->linkExists('Add API Doc');
$this
->clickLink('Add API Doc');
$assert
->fieldValueEquals('title[0][value]', '');
$assert
->fieldValueEquals('body[0][value]', '');
$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('title[0][value]', $random_name);
$page
->fillField('body[0][value]', $random_description);
$page
->fillField('field_apidoc_spec_file_source', 'file');
$this
->getSession()
->getPage()
->find('css', 'input[name="field_apidoc_spec[0][fids]"]')
->setValue($file
->id());
$this
->getSession()
->getPage()
->pressButton(t('Save'));
$assert
->statusCodeEquals(200);
$assert
->pageTextContains(new FormattableMarkup('API Doc @name has been created.', [
'@name' => $random_name,
]));
$assert
->linkExists($random_name);
$assert
->linkExists('Edit');
$assert
->linkExists('Delete');
$this
->clickLink('Edit');
$assert
->statusCodeEquals(200);
$assert
->fieldValueEquals('title[0][value]', $random_name);
$assert
->fieldValueEquals('body[0][value]', $random_description);
$assert
->linkExists('specA.yml');
$this
->clickLink('Delete');
}
}