You are here

protected function JsonApiFunctionalTestBase::createDefaultContent in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/jsonapi/tests/src/Functional/JsonApiFunctionalTestBase.php \Drupal\Tests\jsonapi\Functional\JsonApiFunctionalTestBase::createDefaultContent()

Creates default content to test the API.

Parameters

int $num_articles: Number of articles to create.

int $num_tags: Number of tags to create.

bool $article_has_image: Set to TRUE if you want to add an image to the generated articles.

bool $article_has_link: Set to TRUE if you want to add a link to the generated articles.

bool $is_multilingual: (optional) Set to TRUE if you want to enable multilingual content.

bool $referencing_twice: (optional) Set to TRUE if you want articles to reference the same tag twice.

4 calls to JsonApiFunctionalTestBase::createDefaultContent()
JsonApiFunctionalMultilingualTest::setUp in core/modules/jsonapi/tests/src/Functional/JsonApiFunctionalMultilingualTest.php
JsonApiFunctionalTest::testRead in core/modules/jsonapi/tests/src/Functional/JsonApiFunctionalTest.php
Tests the GET method.
JsonApiFunctionalTest::testReferencingTwiceRead in core/modules/jsonapi/tests/src/Functional/JsonApiFunctionalTest.php
Tests the GET method on articles referencing the same tag twice.
JsonApiFunctionalTest::testWrite in core/modules/jsonapi/tests/src/Functional/JsonApiFunctionalTest.php
Tests POST, PATCH and DELETE.

File

core/modules/jsonapi/tests/src/Functional/JsonApiFunctionalTestBase.php, line 248

Class

JsonApiFunctionalTestBase
Provides helper methods for the JSON:API module's functional tests.

Namespace

Drupal\Tests\jsonapi\Functional

Code

protected function createDefaultContent($num_articles, $num_tags, $article_has_image, $article_has_link, $is_multilingual, $referencing_twice = FALSE) {
  $random = $this
    ->getRandomGenerator();
  for ($created_tags = 0; $created_tags < $num_tags; $created_tags++) {
    $term = Term::create([
      'vid' => 'tags',
      'name' => $random
        ->name(),
    ]);
    if ($is_multilingual) {
      $term
        ->addTranslation('ca', [
        'name' => $term
          ->getName() . ' (ca)',
      ]);
    }
    $term
      ->save();
    $this->tags[] = $term;
  }
  for ($created_nodes = 0; $created_nodes < $num_articles; $created_nodes++) {
    $values = [
      'uid' => [
        'target_id' => $this->user
          ->id(),
      ],
      'type' => 'article',
    ];
    if ($referencing_twice) {
      $values['field_tags'] = [
        [
          'target_id' => 1,
        ],
        [
          'target_id' => 1,
        ],
      ];
    }
    else {

      // Get N random tags.
      $selected_tags = mt_rand(1, $num_tags);
      $tags = [];
      while (count($tags) < $selected_tags) {
        $tags[] = mt_rand(1, $num_tags);
        $tags = array_unique($tags);
      }
      $values['field_tags'] = array_map(function ($tag) {
        return [
          'target_id' => $tag,
        ];
      }, $tags);
    }
    if ($article_has_image) {
      $file = File::create([
        'uri' => 'public://' . $random
          ->name() . '.png',
      ]);
      $file
        ->setPermanent();
      $file
        ->save();
      $this->files[] = $file;
      $values['field_image'] = [
        'target_id' => $file
          ->id(),
        'alt' => 'alt text',
      ];
    }
    if ($article_has_link) {
      $values['field_link'] = [
        'title' => $this
          ->getRandomGenerator()
          ->name(),
        'uri' => sprintf('%s://%s.%s', 'http' . (mt_rand(0, 2) > 1 ? '' : 's'), $this
          ->getRandomGenerator()
          ->name(), 'org'),
      ];
    }

    // Create values for the sort fields, to allow for testing complex
    // sorting:
    // - field_sort1 increments every 5 articles, starting at zero
    // - field_sort2 decreases every article, ending at zero.
    $values['field_sort1'] = [
      'value' => floor($created_nodes / 5),
    ];
    $values['field_sort2'] = [
      'value' => $num_articles - $created_nodes,
    ];
    $node = $this
      ->createNode($values);
    if ($is_multilingual === static::IS_MULTILINGUAL) {
      $values['title'] = $node
        ->getTitle() . ' (ca)';
      $values['field_image']['alt'] = 'alt text (ca)';
      $node
        ->addTranslation('ca', $values);
    }
    $node
      ->save();
    $this->nodes[] = $node;
  }
  if ($article_has_link) {

    // Make sure that there is at least 1 https link for ::testRead() #19.
    $this->nodes[0]->field_link = [
      'title' => 'Drupal',
      'uri' => 'https://drupal.org',
    ];
    $this->nodes[0]
      ->save();
  }
}