View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\og\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\block_content\Entity\BlockContent;
use Drupal\block_content\Entity\BlockContentType;
use Drupal\node\Entity\Node;
use Drupal\og\Og;
use Drupal\og\OgGroupAudienceHelperInterface;
class OgComplexWidgetTest extends BrowserTestBase {
use ContentTypeCreationTrait;
use NodeCreationTrait;
public static $modules = [
'block_content',
'node',
'og',
];
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
BlockContentType::create([
'id' => 'group',
])
->save();
Og::groupTypeManager()
->addGroup('block_content', 'group');
$this
->createContentType([
'type' => 'post',
]);
$settings = [
'field_storage_config' => [
'settings' => [
'target_type' => 'block_content',
],
],
];
Og::createField(OgGroupAudienceHelperInterface::DEFAULT_FIELD, 'node', 'post', $settings);
}
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',
]);
$values = [
'type' => 'group',
'uid' => $group_owner
->id(),
];
$group = BlockContent::create($values);
$group
->save();
$this
->drupalLogin($admin_user);
$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);
$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);
$post = Node::load($post_nid);
$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.");
}
public function ogComplexFieldsProvider() {
return [
[
'Groups audience',
'og_audience[0][target_id]',
],
[
'Other groups',
'other_groups[0][target_id]',
],
];
}
}