You are here

protected function ConsumerImageSylesFunctionalTest::createDefaultContent in Consumer Image Styles 8

Same name and namespace in other branches
  1. 8.3 tests/src/Functional/ConsumerImageSylesFunctionalTest.php \Drupal\Tests\consumer_image_styles\Functional\ConsumerImageSylesFunctionalTest::createDefaultContent()
  2. 8.2 tests/src/Functional/ConsumerImageSylesFunctionalTest.php \Drupal\Tests\consumer_image_styles\Functional\ConsumerImageSylesFunctionalTest::createDefaultContent()
  3. 4.x tests/src/Functional/ConsumerImageSylesFunctionalTest.php \Drupal\Tests\consumer_image_styles\Functional\ConsumerImageSylesFunctionalTest::createDefaultContent()

Creates default content to test the API.

Parameters

int $num_nodes: Number of articles to create.

1 call to ConsumerImageSylesFunctionalTest::createDefaultContent()
ConsumerImageSylesFunctionalTest::testRead in tests/src/Functional/ConsumerImageSylesFunctionalTest.php
Test the GET method.

File

tests/src/Functional/ConsumerImageSylesFunctionalTest.php, line 81

Class

ConsumerImageSylesFunctionalTest
@group consumer_image_styles

Namespace

Drupal\Tests\consumer_image_styles\Functional

Code

protected function createDefaultContent($num_nodes) {
  $random = $this
    ->getRandomGenerator();
  for ($created_nodes = 0; $created_nodes < $num_nodes; $created_nodes++) {
    $file = File::create([
      'uri' => 'public://' . $random
        ->name() . '.png',
    ]);
    $file
      ->setPermanent();
    $file
      ->save();
    $this->files[] = $file;
    $values = [
      'uid' => [
        'target_id' => $this->user
          ->id(),
      ],
      'type' => $this->contentType
        ->id(),
    ];
    $values[$this->imageFieldName] = [
      'target_id' => $file
        ->id(),
      'alt' => 'alt text',
    ];
    $node = $this
      ->createNode($values);
    $this->nodes[] = $node;
  }

  // Create the image styles.
  $image_styles = array_map(function ($name) {
    $image_style = ImageStyle::create([
      'name' => $name,
    ]);
    $image_style
      ->save();
    return $image_style;
  }, [
    'foo',
    'bar',
  ]);

  // Create the consumer.
  $this->consumer = Consumer::create([
    'owner_id' => '',
    'label' => $this
      ->getRandomGenerator()
      ->name(),
    'image_styles' => array_map(function (ImageStyle $image_style) {
      return [
        'target_id' => $image_style
          ->id(),
      ];
    }, $image_styles),
  ]);
  $this->consumer
    ->save();
}