b34321f3fc719dcb4bf30af088b0ce67e7854147
[yaffs-website] / web / modules / contrib / paragraphs / tests / src / Kernel / ParagraphsBehaviorPluginsTest.php
1 <?php
2
3 namespace Drupal\Tests\paragraphs\Kernel;
4
5 use Drupal\paragraphs\Entity\Paragraph;
6 use Drupal\paragraphs\Entity\ParagraphsType;
7 use Drupal\KernelTests\KernelTestBase;
8
9 /**
10  * Tests the behavior plugins API.
11  *
12  * @group paragraphs
13  */
14 class ParagraphsBehaviorPluginsTest extends KernelTestBase {
15
16   /**
17    * Modules to enable.
18    *
19    * @var array
20    */
21   public static $modules = [
22     'paragraphs',
23     'user',
24     'system',
25     'field',
26     'entity_reference_revisions',
27     'paragraphs_test',
28   ];
29
30   /**
31    * {@inheritdoc}
32    */
33   protected function setUp() {
34     parent::setUp();
35     $this->installEntitySchema('user');
36     $this->installEntitySchema('paragraph');
37     $this->installSchema('system', ['sequences']);
38     \Drupal::moduleHandler()->loadInclude('paragraphs', 'install');
39   }
40
41   /**
42    * Tests the behavior settings API.
43    */
44   public function testBehaviorSettings() {
45     // Create a paragraph type.
46     $paragraph_type = ParagraphsType::create(array(
47       'label' => 'test_text',
48       'id' => 'test_text',
49       'behavior_plugins' => [
50         'test_text_color' => [
51           'enabled' => TRUE,
52         ]
53       ],
54     ));
55     $paragraph_type->save();
56
57     // Create a paragraph and set its feature settings.
58     $paragraph = Paragraph::create([
59       'type' => 'test_text',
60     ]);
61     $feature_settings = [
62       'test_text_color' => [
63         'text_color' => 'red'
64       ],
65     ];
66     $paragraph->setAllBehaviorSettings($feature_settings);
67     $paragraph->save();
68
69     // Load the paragraph and assert its stored feature settings.
70     $paragraph = Paragraph::load($paragraph->id());
71     $this->assertEquals($paragraph->getAllBehaviorSettings(), $feature_settings);
72
73     // Check the text color plugin settings summary.
74     $plugin = $paragraph->getParagraphType()->getBehaviorPlugins()->getEnabled();
75     $this->assertEquals($plugin['test_text_color']->settingsSummary($paragraph)[0], 'Text color: red');
76
77     // Update the value of an specific plugin.
78     $paragraph->setBehaviorSettings('test_text_color', ['text_color' => 'blue']);
79     $paragraph->save();
80
81     // Assert the values have been updated.
82     $paragraph = Paragraph::load($paragraph->id());
83     $this->assertEquals($paragraph->getBehaviorSetting('test_text_color', 'text_color'), 'blue');
84
85     // Check the text color plugin settings summary.
86     $plugin = $paragraph->getParagraphType()->getBehaviorPlugins()->getEnabled();
87     $this->assertEquals($plugin['test_text_color']->settingsSummary($paragraph)[0], 'Text color: blue');
88
89   }
90
91 }