You are here

public function UITest::testEntityTypeListingOperations in Entity Construction Kit (ECK) 8

Makes sure the operations on the entity type listing page work as expected.

File

tests/src/Functional/UITest.php, line 75

Class

UITest
Tests if eck's UI elements are working properly.

Namespace

Drupal\Tests\eck\Functional

Code

public function testEntityTypeListingOperations() {
  $entityTypeManager = \Drupal::entityTypeManager();
  $entity = $entityTypeManager
    ->getDefinition('eck_entity_type');
  $this
    ->drupalGet(Url::fromRoute('eck.entity_type.list'));
  $noEntitiesYetText = (string) $this
    ->t('There are no @label entities yet.', [
    '@label' => strtolower($entity
      ->getLabel()),
  ]);
  $this
    ->assertSession()
    ->responseContains($noEntitiesYetText);
  $entityType = $this
    ->createEntityType();
  $this
    ->drupalGet(Url::fromRoute('eck.entity_type.list'));
  $this
    ->assertSession()
    ->responseNotContains($noEntitiesYetText);
  foreach ([
    'Add content',
    'Content list',
  ] as $option) {
    $this
      ->assertSession()
      ->linkNotExists($this
      ->t($option), $this
      ->t('No %option option is shown when there are no bundles.', [
      '%option' => $this
        ->t($option),
    ]));
  }
  $this
    ->assertSession()
    ->linkExists($this
    ->t('Add bundle'));
  $this
    ->assertSession()
    ->linkExists($this
    ->t('Bundle list'));
  $this
    ->assertSession()
    ->linkExists($this
    ->t('Edit'));
  $this
    ->assertSession()
    ->linkExists($this
    ->t('Delete'));
  $bundles[] = $this
    ->createEntityBundle($entityType['id'], $entityType['id']);
  $this
    ->drupalGet(Url::fromRoute('eck.entity_type.list'));
  $this
    ->assertSession()
    ->responseNotContains($noEntitiesYetText);
  $this
    ->assertSession()
    ->linkNotExists($this
    ->t('Content list'), $this
    ->t('No %option option is shown when there is no content.', [
    '%option' => $this
      ->t('Content list'),
  ]));
  $this
    ->assertSession()
    ->linkExists($this
    ->t('Add content'));
  $this
    ->assertSession()
    ->linkExists($this
    ->t('Bundle list'));
  $this
    ->assertSession()
    ->linkExists($this
    ->t('Edit'));
  $this
    ->assertSession()
    ->linkExists($this
    ->t('Delete'));

  // Since there is only one bundle. The add content link should point
  // directly to the correct add entity form. We should be able to add a new
  // entity directly after clicking the link.
  $this
    ->clickLink($this
    ->t('Add content'));
  $this
    ->submitForm([
    'title[0][value]' => $this
      ->randomMachineName(),
  ], 'Save');

  // There is now content in the datbase, which means the content list link
  // should also be displayed.
  $this
    ->drupalGet(Url::fromRoute('eck.entity_type.list'));
  $this
    ->assertSession()
    ->responseNotContains($noEntitiesYetText);
  $this
    ->assertSession()
    ->linkExists($this
    ->t('Content list'));
  $this
    ->assertSession()
    ->linkExists($this
    ->t('Add content'));
  $this
    ->assertSession()
    ->linkExists($this
    ->t('Bundle list'));
  $this
    ->assertSession()
    ->linkExists($this
    ->t('Edit'));
  $this
    ->assertSession()
    ->linkExists($this
    ->t('Delete'));

  // If there are multiple bundles, clicking the add Content button should end
  // up with a choice between all available bundles.
  $bundles[] = $this
    ->createEntityBundle($entityType['id']);
  $this
    ->drupalGet(Url::fromRoute('eck.entity_type.list'));
  $this
    ->clickLink($this
    ->t('Add content'));
  foreach ($bundles as $bundle) {
    $this
      ->assertSession()
      ->responseContains($bundle['name']);
  }
}