You are here

public function ApiDocsAdminTest::testApiDocAdministration in Apigee API Catalog 8

Same name and namespace in other branches
  1. 8.2 tests/src/Functional/ApiDocsAdminTest.php \Drupal\Tests\apigee_api_catalog\Functional\ApiDocsAdminTest::testApiDocAdministration()

Tests that a user can administer API Doc entities.

File

tests/src/Functional/ApiDocsAdminTest.php, line 90

Class

ApiDocsAdminTest
Simple test to ensure that main page loads with module enabled.

Namespace

Drupal\Tests\apigee_api_catalog\Functional

Code

public function testApiDocAdministration() {
  $header_selector = 'table .empty';
  $assert = $this
    ->assertSession();

  // Get the API Doc admin page.
  $this
    ->drupalGet(Url::fromRoute('entity.apidoc.collection'));

  // No API docs yet.
  $assert
    ->elementTextContains('css', $header_selector, 'There are no API docs yet.');

  // User can add entity content.
  $assert
    ->linkExists('Add API Doc');
  $this
    ->clickLink('Add API Doc');

  // Fields should have proper defaults.
  $assert
    ->fieldValueEquals('name[0][value]', '');
  $assert
    ->fieldValueEquals('description[0][value]', '');
  $assert
    ->fieldValueEquals('status[value]', '1');

  // Create a new spec in site.
  $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'");

  // Save it, inserting a new record.
  $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);

  // Can't use drupalPostForm() to set hidden fields.
  $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,
  ]));

  // Entity listed.
  $assert
    ->linkExists($random_name);
  $assert
    ->linkExists('Edit');
  $assert
    ->linkExists('Delete');

  // Click on API Doc to edit.
  $this
    ->clickLink('Edit');
  $assert
    ->statusCodeEquals(200);

  // Edit form should have proper values.
  $assert
    ->fieldValueEquals('name[0][value]', $random_name);
  $assert
    ->fieldValueEquals('description[0][value]', $random_description);
  $assert
    ->fieldValueEquals('status[value]', '1');
  $assert
    ->linkExists('specA.yml');

  // Delete the entity.
  $this
    ->clickLink('Delete');

  // Confirm deletion.
  $assert
    ->linkExists('Cancel');
  $this
    ->drupalPostForm(NULL, [], 'Delete');

  // Back to list, should not longer have API Doc.
  $assert
    ->pageTextNotContains($random_name);
}