You are here

public function OgComplexWidgetTest::testFields in Organic groups 8

Tests adding groups with the "Groups audience" and "Other Groups" fields.

@dataProvider ogComplexFieldsProvider

File

tests/src/Functional/OgComplexWidgetTest.php, line 66

Class

OgComplexWidgetTest
Tests the complex widget.

Namespace

Drupal\Tests\og\Functional

Code

public function testFields($field, $field_name) {
  $admin_user = $this
    ->drupalCreateUser([
    'administer organic groups',
    'access content',
    'create post content',
  ]);
  $group_owner = $this
    ->drupalCreateUser([
    'access content',
    'create post content',
  ]);

  // Create a group content type owned by the group owner.
  $values = [
    'type' => 'group',
    'uid' => $group_owner
      ->id(),
  ];
  $group = BlockContent::create($values);
  $group
    ->save();

  // Log in as administrator.
  $this
    ->drupalLogin($admin_user);

  // Create a new post in the group by using the given field in the UI.
  $edit = [
    'title[0][value]' => "Group owner's post.",
    $field_name => "group ({$group->id()})",
  ];
  $this
    ->drupalGet('node/add/post');
  $this
    ->submitForm($edit, 'Save');
  $this
    ->assertSession()
    ->statusCodeEquals(200);

  // Retrieve the post that was created from the database.
  $query = $this->container
    ->get('entity_type.manager')
    ->getStorage('node')
    ->getQuery();
  $result = $query
    ->condition('type', 'post')
    ->range(0, 1)
    ->sort('nid', 'DESC')
    ->execute();
  $post_nid = reset($result);

  /** @var \Drupal\node\NodeInterface $post */
  $post = Node::load($post_nid);

  // Check that the post references the group correctly.

  /** @var \Drupal\og\OgMembershipReferenceItemList $reference_list */
  $reference_list = $post
    ->get(OgGroupAudienceHelperInterface::DEFAULT_FIELD);
  $this
    ->assertEquals(1, $reference_list
    ->count(), "There is 1 reference after adding a group to the '{$field}' field.");
  $this
    ->assertEquals($group
    ->id(), $reference_list
    ->first()
    ->getValue()['target_id'], "The '{$field}' field references the correct group.");
}