<?php
namespace Drupal\Tests\paragraphs\Functional\WidgetLegacy;
use Drupal\paragraphs\Entity\ParagraphsType;
class ParagraphsAddModesTest extends ParagraphsTestBase {
public function testNoDefaultValue() {
$this
->loginAsAdmin();
$this
->addParagraphedContentType('paragraphed_test', 'paragraphs_field', 'entity_reference_paragraphs');
$this
->drupalGet('admin/structure/types/manage/paragraphed_test/fields');
$this
->clickLink('Edit');
$this
->assertSession()
->pageTextContains('No widget available for: paragraphs_field.');
$this
->submitForm([], 'Save settings');
$this
->assertSession()
->pageTextContains('Saved paragraphs_field configuration.');
$this
->assertSession()
->statusCodeEquals(200);
}
public function testEmptyAllowedTypes() {
$this
->loginAsAdmin();
$this
->addParagraphedContentType('paragraphed_test', 'paragraphs', 'entity_reference_paragraphs');
$this
->drupalGet('admin/structure/types/manage/paragraphed_test/fields');
$this
->clickLink('Edit');
$this
->submitForm([], 'Save settings');
$this
->assertSession()
->pageTextContains('Saved paragraphs configuration.');
}
public function testDropDownMode() {
$this
->loginAsAdmin();
$this
->addParagraphsType('btext');
$this
->addParagraphsType('dtext');
$this
->addParagraphedContentType('paragraphed_test', 'paragraphs', 'entity_reference_paragraphs');
$this
->drupalGet('admin/structure/types/manage/paragraphed_test/fields/node.paragraphed_test.paragraphs');
$this
->submitForm([], 'Save settings');
$this
->setAddMode('paragraphed_test', 'paragraphs', 'dropdown');
$this
->assertAddButtons([
'Add btext',
'Add dtext',
]);
$this
->addParagraphsType('atext');
$this
->assertAddButtons([
'Add btext',
'Add dtext',
'Add atext',
]);
$this
->setParagraphsTypeWeight('paragraphed_test', 'dtext', 2, 'paragraphs');
$this
->assertAddButtons([
'Add dtext',
'Add btext',
'Add atext',
]);
$this
->setAllowedParagraphsTypes('paragraphed_test', [
'dtext',
'atext',
], TRUE, 'paragraphs');
$this
->assertAddButtons([
'Add dtext',
'Add atext',
]);
$this
->setParagraphsTypeWeight('paragraphed_test', 'atext', 1, 'paragraphs');
$this
->assertAddButtons([
'Add atext',
'Add dtext',
]);
$this
->setAllowedParagraphsTypes('paragraphed_test', [
'atext',
'dtext',
'btext',
], TRUE, 'paragraphs');
$this
->assertAddButtons([
'Add atext',
'Add dtext',
'Add btext',
]);
}
public function testSelectMode() {
$this
->loginAsAdmin();
$this
->addParagraphsType('btext');
$this
->addParagraphsType('dtext');
$this
->addParagraphedContentType('paragraphed_test', 'paragraphs', 'entity_reference_paragraphs');
$this
->drupalGet('admin/structure/types/manage/paragraphed_test/fields/node.paragraphed_test.paragraphs');
$this
->submitForm([], 'Save settings');
$this
->setAddMode('paragraphed_test', 'paragraphs', 'select');
$this
->assertSelectOptions([
'btext',
'dtext',
], 'paragraphs');
$this
->addParagraphsType('atext');
$this
->assertSelectOptions([
'btext',
'dtext',
'atext',
], 'paragraphs');
$this
->setParagraphsTypeWeight('paragraphed_test', 'dtext', 2, 'paragraphs');
$this
->assertSelectOptions([
'dtext',
'btext',
'atext',
], 'paragraphs');
$this
->setAllowedParagraphsTypes('paragraphed_test', [
'dtext',
'atext',
], TRUE, 'paragraphs');
$this
->assertSelectOptions([
'dtext',
'atext',
], 'paragraphs');
$this
->setParagraphsTypeWeight('paragraphed_test', 'atext', 1, 'paragraphs');
$this
->assertSelectOptions([
'atext',
'dtext',
], 'paragraphs');
$this
->setAllowedParagraphsTypes('paragraphed_test', [
'atext',
'dtext',
'btext',
], TRUE, 'paragraphs');
$this
->assertSelectOptions([
'atext',
'dtext',
'btext',
], 'paragraphs');
}
protected function assertAddButtons($options) {
$this
->drupalGet('node/add/paragraphed_test');
$buttons = $this
->xpath('//input[@class="field-add-more-submit button--small button js-form-submit form-submit"]');
foreach ($buttons as $key => $button) {
$this
->assertEquals($button
->getValue(), $options[$key]);
}
$this
->assertEquals(count($buttons), count($options), 'The amount of drop down options matches with the given array');
}
protected function assertSelectOptions($options, $paragraphs_field) {
$this
->drupalGet('node/add/paragraphed_test');
$buttons = $this
->xpath('//*[@name="' . $paragraphs_field . '[add_more][add_more_select]"]/option');
foreach ($buttons as $key => $button) {
$this
->assertEquals($button
->getValue(), $options[$key]);
}
$this
->assertEquals(count($buttons), count($options), 'The amount of select options matches with the given array');
$this
->assertNotEquals($this
->xpath('//*[@name="' . $paragraphs_field . '_add_more"]'), [], 'The add button is displayed');
}
public function testSettingDefaultParagraphType() {
$this
->addParagraphedContentType('paragraphed_test', 'paragraphs', 'entity_reference_paragraphs');
$this
->loginAsAdmin([
'administer content types',
'administer node form display',
'edit any paragraphed_test content',
]);
$paragraphs_type_text_image = ParagraphsType::create([
'id' => 'text_image',
'label' => 'Text + Image',
]);
$paragraphs_type_text = ParagraphsType::create([
'id' => 'text',
'label' => 'Text',
]);
$paragraphs_type_text_image
->save();
$paragraphs_type_text
->save();
$this
->setDefaultParagraphType('paragraphed_test', 'paragraphs', 'paragraphs_settings_edit', 'text_image');
$this
->drupalGet('node/add/paragraphed_test');
$this
->assertSession()
->pageTextContains('Text + Image');
$this
->removeDefaultParagraphType('paragraphed_test');
$this
->setDefaultParagraphType('paragraphed_test', 'paragraphs', 'paragraphs_settings_edit', '_none');
$this
->drupalGet('node/add/paragraphed_test');
$this
->assertSession()
->pageTextContains('No Paragraph added yet.');
$this
->setDefaultParagraphType('paragraphed_test', 'paragraphs', 'paragraphs_settings_edit', 'text_image');
$this
->removeDefaultParagraphType('paragraphed_test');
$edit = [
'title[0][value]' => 'New Host',
];
$this
->submitForm($edit, 'Save');
$this
->drupalGet('node/1/edit');
$this
->assertSession()
->pageTextContains('No Paragraph added yet.');
}
public function testDefaultParagraphTypeWithSingleType() {
$this
->addParagraphedContentType('paragraphed_test', 'paragraphs', 'entity_reference_paragraphs');
$this
->loginAsAdmin([
'administer content types',
'administer node form display',
'edit any paragraphed_test content',
]);
$paragraphs_type_text = ParagraphsType::create([
'id' => 'text',
'label' => 'Text',
]);
$paragraphs_type_text
->save();
$this
->drupalGet('node/add/paragraphed_test');
$this
->assertSession()
->pageTextNotContains('No Paragraph added yet.');
$this
->setDefaultParagraphType('paragraphed_test', 'paragraphs', 'paragraphs_settings_edit', '_none');
$this
->drupalGet('node/add/paragraphed_test');
$this
->assertSession()
->pageTextContains('No Paragraph added yet.');
}
}