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

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

namespace Drupal\Tests\outline\Functional;

use Drupal\Component\Render\FormattableMarkup;
use Drupal\outline\Entity\Entry;

/**
 * Tests the loading of multiple outline entries at once.
 *
 * @group outline
 */
class LoadMultipleTest extends OutlineTestBase {

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

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

  /**
   * Create a outline and some outline entries, ensuring they're loaded
   * correctly using entity_load_multiple().
   */
  public function testOutlineEntryMultipleLoad() {
    // Create a outline.
    $outline = $this->createOutline();

    // Create five entries in the outline.
    $i = 0;
    while ($i < 5) {
      $i++;
      $this->createEntry($outline);
    }
    // Load the entries from the outline.
    $entry_storage = \Drupal::entityTypeManager()->getStorage('outline_entry');
    $entries = $entry_storage->loadByProperties(['oid' => $outline->id()]);
    $count = count($entries);
    $this->assertEqual($count, 5, new FormattableMarkup('Correct number of entries were loaded. @count entries.', ['@count' => $count]));

    // Load the same entries again by tid.
    $entries2 = Entry::loadMultiple(array_keys($entries));
    $this->assertEqual($count, count($entries2), 'Five entries were loaded by tid.');
    $this->assertEqual($entries, $entries2, 'Both arrays contain the same entries.');

    // Remove one entry from the array, then delete it.
    $deleted = array_shift($entries2);
    $deleted->delete();
    $deleted_entry = Entry::load($deleted->id());
    $this->assertNull($deleted_entry);

    // Load entries from the outline by vid.
    $entries3 = $entry_storage->loadByProperties(['oid' => $outline->id()]);
    $this->assertCount(4, $entries3, 'Correct number of entries were loaded.');
    $this->assertFalse(isset($entries3[$deleted->id()]));

    // Create a single entry and load it by name.
    $entry = $this->createEntry($outline);
    $loaded_entries = $entry_storage->loadByProperties(['name' => $entry->getName()]);
    $this->assertCount(1, $loaded_entries, 'One entry was loaded.');
    $loaded_entry = reset($loaded_entries);
    $this->assertEqual($entry->id(), $loaded_entry->id(), 'Entry loaded by name successfully.');
  }

}

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

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