You are here

public function ContextTest::setUp in Message Subscribe 8

Overrides MessageSubscribeTestBase::setUp

File

tests/src/Kernel/ContextTest.php, line 81

Class

ContextTest
Test getting context from entity.

Namespace

Drupal\Tests\message_subscribe\Kernel

Code

public function setUp() {
  parent::setUp();
  $this
    ->installSchema('comment', [
    'comment_entity_statistics',
  ]);
  $this
    ->installEntitySchema('comment');
  $this
    ->installEntitySchema('og_membership');
  $this
    ->installEntitySchema('taxonomy_term');
  $this
    ->installConfig([
    'comment',
    'og',
  ]);
  foreach (range(1, 3) as $uid) {
    $this->users[$uid] = $this
      ->createUser();
  }

  // Create group node-type.
  $type = $this
    ->createContentType();
  $group_type = $type
    ->id();
  Og::groupTypeManager()
    ->addGroup('node', $group_type);

  // Create node-type.
  $type = $this
    ->createContentType();
  $node_type = $type
    ->id();
  Og::createField(OgGroupAudienceHelper::DEFAULT_FIELD, 'node', $node_type);

  // Enable comments on the node type.
  $this
    ->addDefaultCommentField('node', $node_type);

  // Create vocabulary and terms.
  $vocabulary = Vocabulary::create([
    'vid' => 'terms',
    'name' => 'Terms',
  ]);
  $vocabulary
    ->save();

  // Create terms.
  foreach (range(1, 3) as $i) {
    $this->terms[$i] = Term::create([
      'name' => "term {$i}",
      'vid' => $vocabulary
        ->id(),
    ]);
    $this->terms[$i]
      ->save();
  }

  // Create a multiple terms-reference field.
  $this
    ->createEntityReferenceField('node', $node_type, 'field_terms_ref', $this
    ->randomString(), 'taxonomy_term', 'default', [], FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);

  // Create OG group.
  $settings = [];
  $settings['type'] = $group_type;
  $settings['uid'] = $this->users[3]
    ->id();
  $this->group = $this
    ->createNode($settings);

  // Create node.
  $settings = [
    'type' => $node_type,
    'uid' => $this->users[1]
      ->id(),
    'field_terms_ref' => $this->terms,
    OgGroupAudienceHelper::DEFAULT_FIELD => [
      'target_id' => $this->group
        ->id(),
    ],
  ];
  $this->node = $this
    ->createNode($settings);

  // Add comment.
  $settings = [
    'subject' => 'topic',
    'entity_type' => 'node',
    'entity_id' => $this->node
      ->id(),
    'uid' => $this->users[2]
      ->id(),
    'field_name' => 'comment',
    'status' => CommentInterface::PUBLISHED,
  ];
  $this->comment = Comment::create($settings);
  $this->comment
    ->save();
  $this->subscribers = $this->container
    ->get('message_subscribe.subscribers');
}