View source
<?php
namespace Drupal\Tests\paragraphs_collection_demo\Functional;
use Drupal\Tests\field_ui\Traits\FieldUiTestTrait;
use Drupal\Tests\paragraphs\Functional\WidgetStable\ParagraphsTestBase;
class ParagraphsBackgroundPluginTest extends ParagraphsTestBase {
use FieldUiTestTrait;
protected $defaultTheme = 'classy';
public static $modules = [
'paragraphs_collection_demo',
'image',
'paragraphs_collection_test',
];
public function testBackgroundImageSelection() {
$this
->addParagraphedContentType('article', 'paragraphs');
$this
->loginAsAdmin();
$this
->addParagraphsType('text_test');
$bundle_path = 'admin/structure/paragraphs_type/text_test';
$this
->fieldUIAddExistingField($bundle_path, 'paragraphs_text');
$bundle_path = 'admin/structure/paragraphs_type/container';
$this
->fieldUIAddNewField($bundle_path, 'second_background_image', 'Second BG image', 'image', [], []);
$edit = [
'settings[alt_field_required]' => FALSE,
];
$edit_path = 'admin/structure/paragraphs_type/container/fields/paragraph.container.field_second_background_image';
$this
->drupalPostForm($edit_path, $edit, 'Save settings');
$this
->drupalGet('node/add/article');
$this
->drupalPostForm(NULL, [], 'paragraphs_container_add_more');
$background_image = $this
->getTestFiles('image')[0];
$edit = [
'files[paragraphs_0_subform_paragraphs_background_image_0]' => $background_image->uri,
];
$this
->drupalPostForm(NULL, $edit, t('Upload'));
$this
->drupalPostForm(NULL, [], 'paragraphs_0_subform_paragraphs_container_paragraphs_text_test_add_more');
$background_image = $this
->getTestFiles('image')[1];
$edit = [
'files[paragraphs_0_subform_field_second_background_image_0]' => $background_image->uri,
];
$this
->drupalPostForm(NULL, $edit, t('Upload'));
$this
->drupalPostForm(NULL, [], 'paragraphs_0_subform_paragraphs_container_paragraphs_text_test_add_more');
$edit = [
'title[0][value]' => 'Test article',
'paragraphs[0][subform][paragraphs_container_paragraphs][0][subform][paragraphs_text][0][value]' => "This is a non background element",
'paragraphs[0][subform][paragraphs_background_image][0][alt]' => 'This is the alternative text',
];
$this
->drupalPostForm(NULL, $edit, 'Save');
$save_url = $this
->getUrl();
$edit = [
'behavior_plugins[background][settings][background_image_field]' => 'paragraphs_background_image',
];
$this
->drupalPostForm('admin/structure/paragraphs_type/container', $edit, 'Save');
$this
->drupalGet($save_url);
$this
->assertRaw('paragraphs-behavior-background--image field field--name-paragraphs-background-image');
$this
->assertRaw('paragraphs-behavior-background--element field field--name-field-second-background-image');
$edit = [
'behavior_plugins[background][settings][background_image_field]' => 'field_second_background_image',
];
$this
->drupalPostForm('admin/structure/paragraphs_type/container', $edit, 'Save');
$this
->drupalGet($save_url);
$this
->assertRaw('paragraphs-behavior-background--image field field--name-field-second-background-image');
$this
->assertRaw('paragraphs-behavior-background--element field field--name-paragraphs-background-image');
$this
->drupalGet('node/add/article');
$this
->drupalPostForm(NULL, [], 'paragraphs_container_add_more');
$edit = [
'title[0][value]' => 'Test article',
];
$this
->drupalPostForm(NULL, $edit, 'Save');
$this
->addParagraphsType('image_test');
$bundle_path = 'admin/structure/paragraphs_type/image_test';
static::fieldUIAddNewField($bundle_path, 'image', 'Image', 'image', [], []);
$this
->drupalGet('admin/structure/paragraphs_type/image_test');
$this
->assertOption('edit-behavior-plugins-background-settings-background-image-field', '');
$this
->assertOptionSelected('edit-behavior-plugins-background-settings-background-image-field', 'field_image');
$edit = [
'behavior_plugins[background][enabled]' => TRUE,
'behavior_plugins[background][settings][background_image_field]' => '',
];
$this
->drupalPostForm(NULL, $edit, t('Save'));
$this
->assertText('The Background plugin cannot be enabled without an image field.');
}
public function testNoImageField() {
$this
->addParagraphedContentType('article', 'paragraphs');
$this
->loginAsAdmin();
$this
->addParagraphsType('text_test');
$edit = [
'behavior_plugins[background][enabled]' => TRUE,
];
$this
->drupalPostForm('admin/structure/paragraphs_type/text_test', $edit, t('Save'));
$this
->assertText('The Background plugin cannot be enabled without an image field.');
$this
->assertText('No image field type available. Please add at least one in the Manage fields page.');
}
}