outline-8.x-1.x-dev/tests/src/Kernel/EntryKernelTest.php

tests/src/Kernel/EntryKernelTest.php
<?php

namespace Drupal\Tests\outline\Kernel;

use Drupal\outline\Entity\Entry;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\outline\Traits\OutlineTestTrait;

/**
 * Kernel tests for outline entry functions.
 *
 * @group outline
 */
class EntryKernelTest extends KernelTestBase {

  use OutlineTestTrait;

  /**
   * {@inheritdoc}
   */
  protected static $modules = ['filter', 'outline', 'text', 'user', 'dynamic_entity_reference'];

  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::setUp();
    $this->installConfig(['filter']);
    $this->installEntitySchema('outline_entry');
  }

  /**
   * Tests that a deleted entry is no longer in the outline.
   *
   * @throws \Drupal\Core\Entity\EntityStorageException
   */
  public function testEntryDelete() {
    $outline = $this->createOutline();
    $entry = $this->createEntry($outline);
    // Delete entry.
    $entry->delete();
    $entries = \Drupal::entityTypeManager()->getStorage('outline_entry')->loadByProperties(['oid' => $outline->id()]);
    $this->assertTrue(empty($entries), 'Outline is empty after deletion');
  }

  /**
   * Test a outline with multiple entries.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   * @throws \Drupal\Core\Entity\EntityStorageException
   * @throws \Exception
   */
  public function testOutlineTree() {

    // Create a new outline with 10 entries.
    $outline = $this->createOutline();
    $entry = [];
    for ($i = 0; $i < 10; $i++) {
      $entry[$i] = $this->createEntry($outline);
    }

    // $entry[2] is a child of 1.
    $entry[2]->parent = $entry[1]->id();
    $entry[2]->save();
    // $entry[3] is a child of 2.
    $entry[3]->parent = $entry[2]->id();
    $entry[3]->save();
    // $entry[5] is a child of 4.
    $entry[5]->parent = $entry[4]->id();
    $entry[5]->save();
    // $entry[6] is a child of 4.
    $entry[6]->parent = $entry[4]->id();
    $entry[6]->save();

    // Set the weights of some of the top level entries.
    $entry[1]->weight = -3;
    $entry[1]->save();
    $entry[4]->weight = -1;
    $entry[4]->save();
    $entry[0]->weight = 3;
    $entry[0]->save();

    /**
     * Expected tree:
     * entry[1] | depth: 0
     * - entry[2] | depth: 1
     * -- entry[3] | depth: 2
     * entry[4] | depth: 0
     * - entry[5] | depth: 1
     * - entry[6] | depth: 1
     * entry[7] | depth: 0
     * entry[9] | depth: 0
     * entry[9] | depth: 0
     * entry[0] | depth: 0
     */

    // Get the outline entry storage and load the tree.
    /** @var \Drupal\outline\EntryStorageInterface $entry_storage */
    $entry_storage = $this->container->get('entity_type.manager')->getStorage('outline_entry');
    $tree = $entry_storage->loadTree($outline->id());

    // Count all entries.
    $this->assertCount(10, $tree, 'We have all outline tree entries.');

    // Count $entry[1] children.
    $tree = $entry_storage->loadTree($outline->id(), $entry[1]->id(), 1);
    $this->assertCount(1, $tree, 'Entry 1 has 1 child with depth 1.');

    // Count $entry[4] children.
    $tree = $entry_storage->loadTree($outline->id(), $entry[4]->id(), 1);
    $this->assertCount(2, $tree, 'Entry 4 has 2 children with depth 1.');

    // Count elements in every tree depth.
    foreach ($tree as $entry) {
      if (!isset($depth_count[$entry->depth])) {
        $depth_count[$entry->depth] = 0;
      }
      $depth_count[$entry->depth]++;
    }
    $this->assertEqual(6, $depth_count[0], '6 elements in outline tree depth 0.');
    $this->assertEqual(3, $depth_count[1], '3 elements in outline tree depth 1.');
    $this->assertEqual(1, $depth_count[2], '1 element in outline tree depth 2.');
    $this->assertEqual(0, $depth_count[3], '0 elements in outline tree depth 3.');

  }

  /**
   * Tests that a Entry is renderable when unsaved (preview).
   */
  public function testEntryPreview() {
    $entity_manager = \Drupal::entityTypeManager();
    $outline = $this->createOutline();

    // Create a unsaved entry.
    $entry = $entity_manager->getStorage('outline_entry')->create([
      'oid' => $outline->id(),
      'name' => 'Inator',
    ]);

    // Confirm we can get the view of unsaved entry.
    $render_array = $entity_manager->getViewBuilder('outline_entry')
      ->view($entry);
    $this->assertTrue(!empty($render_array), 'Entry view builder is built.');

    // Confirm we can render said view.
    // @TODO: why does next line error out?
    //$rendered = \Drupal::service('renderer')->renderPlain($render_array);
    //$this->assertTrue(!empty(trim($rendered)), 'Entry is able to be rendered.');
  }

}

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

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