You are here

trait ParagraphsLastEntityQueryTrait in Paragraphs 8

Test trait providing helpers to query latest entities created.

Hierarchy

6 files declare their use of ParagraphsLastEntityQueryTrait
ParagraphsClientsideButtonsTest.php in tests/src/FunctionalJavascript/ParagraphsClientsideButtonsTest.php
ParagraphsContentModerationTest.php in modules/paragraphs_library/tests/src/FunctionalJavascript/ParagraphsContentModerationTest.php
ParagraphsContentModerationTranslationsTest.php in tests/src/Functional/WidgetStable/ParagraphsContentModerationTranslationsTest.php
ParagraphsEntityMethodsTest.php in tests/src/Kernel/ParagraphsEntityMethodsTest.php
ParagraphsLegacyContentModerationTranslationsTest.php in tests/src/Functional/WidgetLegacy/ParagraphsLegacyContentModerationTranslationsTest.php

... See full list

File

tests/src/Traits/ParagraphsLastEntityQueryTrait.php, line 8

Namespace

Drupal\Tests\paragraphs\Traits
View source
trait ParagraphsLastEntityQueryTrait {

  /**
   * Gets the latest entity created of a given type.
   *
   * Will fail the test if there is no entity of that type.
   *
   * @param string $entity_type_id
   *   The storage name of the entity.
   * @param bool $load
   *   (optional) Whether or not the return thould be the loaded entity.
   *   Defaults to FALSE.
   *
   * @return mixed
   *   The ID of the latest created entity of that type. If $load is TRUE, will
   *   use ::loadUnchanged() to get a fresh version of the entity object and
   *   return it.
   */
  protected function getLastEntityOfType($entity_type_id, $load = FALSE) {
    $query_result = \Drupal::entityQuery($entity_type_id)
      ->sort('created', 'DESC')
      ->range(0, 1)
      ->execute();
    $entity_id = reset($query_result);
    if (empty($entity_id)) {
      $this
        ->fail('Could not find latest entity of type: ' . $entity_type_id);
    }
    if ($load) {
      return \Drupal::entityTypeManager()
        ->getStorage($entity_type_id)
        ->loadUnchanged($entity_id);
    }
    else {
      return $entity_id;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ParagraphsLastEntityQueryTrait::getLastEntityOfType protected function Gets the latest entity created of a given type.