View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\og_ui\Functional;
use Drupal\Core\Form\FormState;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Tests\BrowserTestBase;
use Drupal\node\Entity\NodeType;
use Drupal\og\Og;
use Drupal\og_ui\BundleFormAlter;
class BundleFormAlterTest extends BrowserTestBase {
public static $modules = [
'block_content',
'entity_test',
'node',
'og_ui',
];
protected $defaultTheme = 'stark';
protected $adminUser;
protected $entityTypeManager;
protected function setUp() : void {
parent::setUp();
$this->entityTypeManager = \Drupal::entityTypeManager();
$this->adminUser = $this
->drupalCreateUser([
'administer blocks',
'administer content types',
'bypass node access',
]);
$this
->drupalLogin($this->adminUser);
}
public function testCreate() {
$edit = [
'label' => 'school',
'id' => 'school',
'og_is_group' => 1,
];
$this
->drupalGet('admin/structure/block/block-content/types/add');
$this
->submitForm($edit, new TranslatableMarkup('Save'));
$edit = [
'name' => 'class',
'type' => 'class',
'og_group_content_bundle' => 1,
'og_target_type' => 'block_content',
'og_target_bundles[]' => [
'school',
],
];
$this
->drupalGet('admin/structure/types/add');
$this
->submitForm($edit, new TranslatableMarkup('Save content type'));
$this->content = $this
->drupalGet('admin/structure/types/manage/class');
$this
->assertTrue($this
->assertSession()
->optionExists('edit-og-target-bundles', 'school')
->isSelected());
$this
->assertTargetType('block_content', 'The target type is set to the "Custom Block" entity type.');
$this
->assertTargetBundles([
'school' => 'school',
], 'The target bundles are set to the "school" bundle.');
$edit = [
'name' => 'class',
'og_group_content_bundle' => 1,
'og_target_type' => 'block_content',
'og_target_bundles[]' => [],
];
$this
->drupalGet('admin/structure/types/manage/class');
$this
->submitForm($edit, new TranslatableMarkup('Save content type'));
$this
->assertTargetBundles(NULL, 'When the target bundle field is cleared from all values, it takes on the value NULL.');
}
public function testGroupContentAjax() {
NodeType::create([
'name' => 'group node',
'type' => 'group',
])
->save();
Og::groupTypeManager()
->addGroup('node', 'group');
Og::groupTypeManager()
->addGroup('entity_test', 'entity_test');
$form = [];
$form_state = new FormState();
$form_state
->setValue('og_target_type', 'entity_test');
$entity = $this->entityTypeManager
->getStorage('node_type')
->create([]);
(new BundleFormAlter($entity))
->formAlter($form, $form_state);
$this
->assertEquals([
'entity_test' => 'Entity Test Bundle',
], $form['og']['og_target_bundles']['#options']);
}
protected function assertTargetBundles($expected, $message) {
$entity_field_manager = $this->container
->get('entity_field.manager');
$entity_field_manager
->clearCachedFieldDefinitions();
$field_definitions = $entity_field_manager
->getFieldDefinitions('node', 'class');
$settings = $field_definitions['og_audience']
->getSetting('handler_settings');
$this
->assertEquals($expected, $settings['target_bundles'], $message);
}
protected function assertTargetType($expected, $message) {
$entity_field_manager = $this->container
->get('entity_field.manager');
$entity_field_manager
->clearCachedFieldDefinitions();
$field_definitions = $entity_field_manager
->getFieldStorageDefinitions('node');
$setting = $field_definitions['og_audience']
->getSetting('target_type');
$this
->assertEquals($expected, $setting, $message);
}
}