View source
<?php
namespace Drupal\Tests\paragraphs_sets\Functional;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\paragraphs_sets\Traits\ParagraphsSetsFunctionalTestTrait;
class ParagraphSetBasicFunctionality extends BrowserTestBase {
use ParagraphsSetsFunctionalTestTrait;
protected $defaultTheme = 'stark';
public static $modules = [
'node',
'paragraphs_sets',
];
protected $sutNodeParagraphField;
protected $sutNodeType;
protected $sutParagraphSetFirst;
protected $sutParagraphSetSecond;
protected $sutParagraphType;
protected $sutParagraphTextField;
protected function setUp() : void {
parent::setUp();
$this->sutParagraphType = mb_strtolower($this
->randomMachineName());
$this
->addParagraphsType($this->sutParagraphType);
$this->sutParagraphTextField = mb_strtolower($this
->randomMachineName());
$this
->addTextFieldInParagraphType($this->sutParagraphTextField, $this->sutParagraphType);
$this->sutParagraphSetFirst = $this
->randomMachineName();
$configFirst = <<<EOF
paragraphs:
- type: {<span class="php-variable">$this</span>-><span class="php-function-or-constant property member-of-self">sutParagraphType</span>}
data:
{<span class="php-variable">$this</span>-><span class="php-function-or-constant property member-of-self">sutParagraphTextField</span>}: One
- type: {<span class="php-variable">$this</span>-><span class="php-function-or-constant property member-of-self">sutParagraphType</span>}
data:
{<span class="php-variable">$this</span>-><span class="php-function-or-constant property member-of-self">sutParagraphTextField</span>}: Two
- type: {<span class="php-variable">$this</span>-><span class="php-function-or-constant property member-of-self">sutParagraphType</span>}
data:
{<span class="php-variable">$this</span>-><span class="php-function-or-constant property member-of-self">sutParagraphTextField</span>}: Three
EOF;
$this
->addParagraphSet($this->sutParagraphSetFirst, $configFirst);
$this->sutParagraphSetSecond = $this
->randomMachineName();
$configSecond = <<<EOF
paragraphs:
- type: {<span class="php-variable">$this</span>-><span class="php-function-or-constant property member-of-self">sutParagraphType</span>}
data:
{<span class="php-variable">$this</span>-><span class="php-function-or-constant property member-of-self">sutParagraphTextField</span>}: Four
- type: {<span class="php-variable">$this</span>-><span class="php-function-or-constant property member-of-self">sutParagraphType</span>}
data:
{<span class="php-variable">$this</span>-><span class="php-function-or-constant property member-of-self">sutParagraphTextField</span>}: Five
EOF;
$this
->addParagraphSet($this->sutParagraphSetSecond, $configSecond);
$this->sutNodeType = mb_strtolower($this
->randomMachineName());
$this
->addNodeType($this->sutNodeType);
$this->sutNodeParagraphField = mb_strtolower($this
->randomMachineName());
$this
->addParagraphRefFieldInNodeType($this->sutNodeParagraphField, $this->sutNodeType);
}
public function testParagraphSetsWithTextFields() {
$this
->drupalLogin($this
->drupalCreateUser([
'administer nodes',
"create {$this->sutNodeType} content",
]));
$this
->drupalGet(Url::fromRoute('node.add', [
'node_type' => $this->sutNodeType,
])
->toString());
$this
->assertSession()
->statusCodeEquals(200);
$page = $this
->getSession()
->getPage();
$page
->hasSelect('Paragraph set');
$page
->hasButton('Select set');
$this
->assertSession()
->pageTextContains(sprintf('for %s', $this->sutNodeParagraphField));
$page
->hasButton('Append set');
$this
->assertSession()
->pageTextContains(sprintf('for %s', $this->sutNodeParagraphField));
$textFieldSelectorSuffix = "[subform][{$this->sutParagraphTextField}][0][value]";
$page
->selectFieldOption('Paragraph set', $this->sutParagraphSetFirst, FALSE);
$page
->pressButton('Select set');
$this
->assertSession()
->elementsCount('css', sprintf('[data-paragraphs-bundle="%s"]', $this->sutParagraphType), 3);
$this
->assertSession()
->fieldValueEquals(sprintf('%s[0]%s', $this->sutNodeParagraphField, $textFieldSelectorSuffix), 'One');
$this
->assertSession()
->fieldValueEquals(sprintf('%s[1]%s', $this->sutNodeParagraphField, $textFieldSelectorSuffix), 'Two');
$this
->assertSession()
->fieldValueEquals(sprintf('%s[2]%s', $this->sutNodeParagraphField, $textFieldSelectorSuffix), 'Three');
$page
->selectFieldOption('Paragraph set', $this->sutParagraphSetSecond, FALSE);
$page
->pressButton('Append set');
$this
->assertSession()
->elementsCount('css', sprintf('[data-paragraphs-bundle="%s"]', $this->sutParagraphType), 5);
$this
->assertSession()
->fieldValueEquals(sprintf('%s[0]%s', $this->sutNodeParagraphField, $textFieldSelectorSuffix), 'One');
$this
->assertSession()
->fieldValueEquals(sprintf('%s[1]%s', $this->sutNodeParagraphField, $textFieldSelectorSuffix), 'Two');
$this
->assertSession()
->fieldValueEquals(sprintf('%s[2]%s', $this->sutNodeParagraphField, $textFieldSelectorSuffix), 'Three');
$this
->assertSession()
->fieldValueEquals(sprintf('%s[3]%s', $this->sutNodeParagraphField, $textFieldSelectorSuffix), 'Four');
$this
->assertSession()
->fieldValueEquals(sprintf('%s[4]%s', $this->sutNodeParagraphField, $textFieldSelectorSuffix), 'Five');
}
}