You are here

public function SocialDrupalContext::createComments in Open Social 10.0.x

Same name and namespace in other branches
  1. 10.3.x tests/behat/features/bootstrap/SocialDrupalContext.php \Drupal\social\Behat\SocialDrupalContext::createComments()
  2. 10.1.x tests/behat/features/bootstrap/SocialDrupalContext.php \Drupal\social\Behat\SocialDrupalContext::createComments()
  3. 10.2.x tests/behat/features/bootstrap/SocialDrupalContext.php \Drupal\social\Behat\SocialDrupalContext::createComments()

Creates comments.

@Given :count comments with text :text for :topic

File

tests/behat/features/bootstrap/SocialDrupalContext.php, line 189

Class

SocialDrupalContext
Provides pre-built step definitions for interacting with Open Social.

Namespace

Drupal\social\Behat

Code

public function createComments($count, $text, $topic) {

  /** @var \Drupal\node\NodeInterface[] $nodes */
  $nodes = \Drupal::entityTypeManager()
    ->getStorage('node')
    ->loadByProperties([
    'title' => $topic,
  ]);
  if (!$nodes) {
    return;
  }
  $node = reset($nodes);
  if ($node
    ->bundle() !== 'topic') {
    return;
  }

  /** @var \Drupal\comment\CommentStorageInterface $storage */
  $storage = \Drupal::entityTypeManager()
    ->getStorage('comment');
  for ($index = 1; $index <= $count; $index++) {
    $storage
      ->create([
      'entity_id' => $node
        ->id(),
      'entity_type' => $node
        ->getEntityTypeId(),
      'field_name' => 'field_topic_comments',
      'field_comment_body' => str_replace('[id]', $index, $text),
      'uid' => $node
        ->getOwnerId(),
    ])
      ->save();
  }
}