You are here

protected function CallbackViewsTest::createNodes in Search Autocomplete 8

Same name and namespace in other branches
  1. 2.x src/Tests/Views/CallbackViewsTest.php \Drupal\search_autocomplete\Tests\Views\CallbackViewsTest::createNodes()

Helper methods: creates a given number of nodes of a give type.

Parameters

integer $number: number of nodes to create.

string $type: the type machine name of nodes to create.

Return value

array the array of node results as it should be in the view result.

1 call to CallbackViewsTest::createNodes()
CallbackViewsTest::testDefaultViewContent in src/Tests/Views/CallbackViewsTest.php
Default view content checks.

File

src/Tests/Views/CallbackViewsTest.php, line 118

Class

CallbackViewsTest
Test callback view configurations.

Namespace

Drupal\search_autocomplete\Tests\Views

Code

protected function createNodes($number, $type, &$expected) {
  $type = $this
    ->drupalCreateContentType([
    'type' => $type,
    'name' => $type,
  ]);
  for ($i = 1; $i < $number; $i++) {
    $settings = [
      'body' => [
        [
          'value' => $this
            ->randomMachineName(32),
          'format' => filter_default_format(),
        ],
      ],
      'type' => $type
        ->id(),
      'created' => 123456789,
      'title' => $type
        ->id() . ' ' . $i,
      'status' => TRUE,
      'promote' => rand(0, 1) == 1,
      'sticky' => rand(0, 1) == 1,
      'uid' => Drupal::currentUser()
        ->id(),
    ];
    $node = Node::create($settings);
    $status = $node
      ->save();
    $this
      ->assertEqual($status, SAVED_NEW, new FormattableMarkup('Created node %title.', [
      '%title' => $node
        ->label(),
    ]));
    $result = [
      'value' => $type
        ->id() . ' ' . $i,
      'fields' => [
        'title' => $type
          ->id() . ' ' . $i,
        'created' => 'by ' . $this->adminUser
          ->getUsername() . ' | Thu, 11/29/1973 - 21:33',
      ],
      'link' => $node
        ->toUrl('canonical', [
        'absolute' => TRUE,
      ])
        ->toString(),
    ];
    if ($i == 1) {
      $result += [
        'group' => [
          'group_id' => strtolower(Html::cleanCssIdentifier($type
            ->label())),
          'group_name' => $type
            ->label() . "s",
        ],
      ];
    }
    $expected[] = $result;
  }
  return $expected;
}