outline-8.x-1.x-dev/tests/src/Functional/OutlineUiTest.php

tests/src/Functional/OutlineUiTest.php
<?php

namespace Drupal\Tests\outline\Functional;

use Drupal\Core\Url;
use Drupal\outline\Entity\Outline;

/**
 * Tests the outline outline interface.
 *
 * @group outline
 */
class OutlineUiTest extends OutlineTestBase {

  /**
   * The outline used for creating entries.
   *
   * @var \Drupal\outline\OutlineInterface
   */
  protected $outline;

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  protected function setUp(): void {
    parent::setUp();
    $this->drupalLogin($this->drupalCreateUser(['administer outline']));
    $this->outline = $this->createOutline();
    $this->drupalPlaceBlock('local_actions_block');
    $this->drupalPlaceBlock('page_title_block');
  }

  /**
   * Create, edit and delete a outline via the user interface.
   */
  public function testOutlineInterface() {
    // Visit the main outline administration page.
    $this->drupalGet('admin/structure/outline');

    // Create a new outline.
    $this->clickLink(t('Add outline'));
    $edit = [];
    $oid = mb_strtolower($this->randomMachineName());
    $edit['name'] = $this->randomMachineName();
    $edit['description'] = $this->randomMachineName();
    $edit['oid'] = $oid;
    $this->drupalPostForm(NULL, $edit, t('Save'));
    $this->assertRaw(t('Created new outline %name.', ['%name' => $edit['name']]), 'Outline created successfully.');

    // Edit the outline.
    $this->drupalGet('admin/structure/outline');
    $this->assertText($edit['name'], 'Outline name found in the outline overview listing.');
    $this->assertText($edit['description'], 'Outline description found in the outline overview listing.');
    $this->assertLinkByHref(Url::fromRoute('entity.outline_entry.add_form', ['outline' => $edit['oid']])->toString());
    $this->clickLink(t('Edit outline'));
    $edit = [];
    $edit['name'] = $this->randomMachineName();
    $edit['description'] = $this->randomMachineName();
    $this->drupalPostForm(NULL, $edit, t('Save'));
    $this->drupalGet('admin/structure/outline');
    $this->assertText($edit['name'], 'Outline name found in the outline overview listing.');
    $this->assertText($edit['description'], 'Outline description found in the outline overview listing.');

    // Try to submit a outline with a duplicate machine name.
    $edit['oid'] = $oid;
    $this->drupalPostForm('admin/structure/outline/add', $edit, t('Save'));
    $this->assertText(t('The machine-readable name is already in use. It must be unique.'));

    // Try to submit an invalid machine name.
    $edit['oid'] = '!&^%';
    $this->drupalPostForm('admin/structure/outline/add', $edit, t('Save'));
    $this->assertText(t('The machine-readable name must contain only lowercase letters, numbers, and underscores.'));

    // Ensure that outline titles are escaped properly.
    $edit = [];
    $edit['name'] = 'Don\'t Panic';
    $edit['description'] = $this->randomMachineName();
    $edit['oid'] = 'don_t_panic';
    $this->drupalPostForm('admin/structure/outline/add', $edit, t('Save'));

    $site_name = $this->config('system.site')->get('name');
    $this->assertTitle("Don't Panic | $site_name");
  }

  /**
   * Changing weights on the outline overview with two or more outlines.
   */
  public function testOutlineAdminChangingWeights() {
    // Create some outlines.
    for ($i = 0; $i < 10; $i++) {
      $this->createOutline();
    }
    // Get all outlines and change their weights.
    $outlines = Outline::loadMultiple();
    $edit = [];
    foreach ($outlines as $key => $outline) {
      $weight = -$outline->get('weight');
      $outlines[$key]->set('weight', $weight);
      $edit['outlines[' . $key . '][weight]'] = $weight;
    }
    // Saving the new weights via the interface.
    $this->drupalPostForm('admin/structure/outline', $edit, t('Save'));

    // Load the outlines from the database.
    $this->container->get('entity_type.manager')->getStorage('outline')->resetCache();
    $new_outlines = Outline::loadMultiple();

    // Check that the weights are saved in the database correctly.
    foreach ($outlines as $key => $outline) {
      $this->assertEqual($new_outlines[$key]->get('weight'), $outlines[$key]->get('weight'), 'The outline weight was changed.');
    }
  }

  /**
   * Test the outline overview with no outlines.
   */
  public function testOutlineAdminNoOutlines() {
    // Delete all outlines.
    $outlines = Outline::loadMultiple();
    foreach ($outlines as $key => $outline) {
      $outline->delete();
    }
    // Confirm that no outlines are found in the database.
    $this->assertEmpty(Outline::loadMultiple(), 'No outlines found.');
    $this->drupalGet('admin/structure/outline');
    // Check the default message for no outlines.
    $this->assertText(t('No outlines available.'));
  }

  /**
   * Deleting a outline.
   */
  public function testOutlineAdminDeletingOutline() {
    // Create a outline.
    $oid = mb_strtolower($this->randomMachineName());
    $edit = [
      'name' => $this->randomMachineName(),
      'oid' => $oid,
    ];
    $this->drupalPostForm('admin/structure/outline/add', $edit, t('Save'));
    $this->assertText(t('Created new outline'), 'New outline was created.');

    // Check the created outline.
    $this->container->get('entity_type.manager')->getStorage('outline')->resetCache();
    $outline = Outline::load($oid);
    $this->assertNotEmpty($outline, 'Outline found.');

    // Delete the outline.
    $this->drupalGet('admin/structure/outline/manage/' . $outline->id());
    $this->clickLink(t('Delete'));
    $this->assertRaw(t('Are you sure you want to delete the outline %name?', ['%name' => $outline->label()]), '[confirm deletion] Asks for confirmation.');
    $this->assertText(t('Deleting a outline will delete all the entries in it. This action cannot be undone.'), '[confirm deletion] Inform that all entries will be deleted.');

    // Confirm deletion.
    $this->drupalPostForm(NULL, NULL, t('Delete'));
    $this->assertRaw(t('Deleted outline %name.', ['%name' => $outline->label()]), 'Outline deleted.');
    $this->container->get('entity_type.manager')->getStorage('outline')->resetCache();
    $this->assertNull(Outline::load($oid), 'Outline not found.');
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc