<?php
namespace Drupal\Tests\paragraphs\Kernel;
use Drupal\paragraphs\Entity\Paragraph;
use Drupal\paragraphs\Entity\ParagraphsType;
use Drupal\KernelTests\KernelTestBase;
class ParagraphsBehaviorPluginsTest extends KernelTestBase {
protected static $modules = [
'paragraphs',
'user',
'system',
'field',
'entity_reference_revisions',
'paragraphs_test',
'file',
];
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('user');
$this
->installEntitySchema('paragraph');
$this
->installSchema('system', [
'sequences',
]);
\Drupal::moduleHandler()
->loadInclude('paragraphs', 'install');
}
public function testBehaviorSettings() {
$paragraph_type = ParagraphsType::create(array(
'label' => 'test_text',
'id' => 'test_text',
'behavior_plugins' => [
'test_text_color' => [
'enabled' => TRUE,
],
],
));
$paragraph_type
->save();
$paragraph = Paragraph::create([
'type' => 'test_text',
]);
$feature_settings = [
'test_text_color' => [
'text_color' => 'red',
],
];
$paragraph
->setAllBehaviorSettings($feature_settings);
$paragraph
->save();
$paragraph = Paragraph::load($paragraph
->id());
$this
->assertEquals($paragraph
->getAllBehaviorSettings(), $feature_settings);
$plugin = $paragraph
->getParagraphType()
->getBehaviorPlugins()
->getEnabled();
$this
->assertEquals($plugin['test_text_color']
->settingsSummary($paragraph)[0], [
'label' => 'Text color',
'value' => 'red',
]);
$paragraph
->setBehaviorSettings('test_text_color', [
'text_color' => 'blue',
]);
$paragraph
->save();
$paragraph = Paragraph::load($paragraph
->id());
$this
->assertEquals($paragraph
->getBehaviorSetting('test_text_color', 'text_color'), 'blue');
$plugin = $paragraph
->getParagraphType()
->getBehaviorPlugins()
->getEnabled();
$this
->assertEquals($plugin['test_text_color']
->settingsSummary($paragraph)[0], [
'label' => 'Text color',
'value' => 'blue',
]);
\Drupal::entityTypeManager()
->getStorage('paragraph')
->resetCache();
$paragraph = Paragraph::load($paragraph
->id());
$paragraph
->setBehaviorSettings('test_another_id', [
'foo' => 'bar',
]);
$paragraph
->save();
$paragraph = Paragraph::load($paragraph
->id());
$settings = $paragraph
->getAllBehaviorSettings();
$this
->assertArrayHasKey('test_text_color', $settings);
$this
->assertArrayHasKey('test_another_id', $settings);
}
public function testBehaviorUninstall() {
$paragraph_type = ParagraphsType::create([
'label' => 'test_text',
'id' => 'test_text',
'behavior_plugins' => [
'test_text_color' => [
'enabled' => TRUE,
],
],
]);
$paragraph_type
->save();
$dependencies = $paragraph_type
->getDependencies();
$plugins = $paragraph_type
->getBehaviorPlugins()
->getInstanceIds();
$this
->assertSame([
'module' => [
'paragraphs_test',
],
], $dependencies);
$this
->assertSame([
'test_text_color' => 'test_text_color',
], $plugins);
$this->container
->get('config.manager')
->uninstall('module', 'paragraphs_test');
$paragraph_type = ParagraphsType::load('test_text');
$this
->assertNotNull($paragraph_type);
$dependencies = $paragraph_type
->getDependencies();
$plugins = $paragraph_type
->getBehaviorPlugins()
->getInstanceIds();
$this
->assertSame([], $dependencies);
$this
->assertSame([], $plugins);
}
}