View source
<?php
namespace Drupal\Tests\thunder\FunctionalJavascript;
use Behat\Mink\Element\DocumentElement;
use Drupal\Component\Utility\Html;
trait ThunderParagraphsTestTrait {
protected function getNumberOfParagraphs($fieldName) {
$paragraphRows = $this
->getParagraphItems($fieldName);
return count($paragraphRows);
}
protected function getParagraphItems($fieldName) {
$fieldNamePart = HTML::cleanCssIdentifier($fieldName);
return $this
->xpath("//*[@id=\"edit-{$fieldNamePart}-wrapper\"]//table[starts-with(@id, \"{$fieldNamePart}-values\")]/tbody/tr[contains(@class, \"draggable\")]//div[number(substring-after(@data-drupal-selector, \"edit-{$fieldNamePart}-\")) >= 0]");
}
public function addParagraph($fieldName, $type, $position = NULL) {
$page = $this
->getSession()
->getPage();
$numberOfParagraphs = $this
->getNumberOfParagraphs($fieldName);
$fieldSelector = HTML::cleanCssIdentifier($fieldName);
if ($position === NULL || $position > $numberOfParagraphs) {
$position = $numberOfParagraphs;
$addButtonCssSelector = "#edit-{$fieldSelector}-wrapper table > tbody > tr:last-child input.paragraphs-features__add-in-between__button";
}
else {
$addButtonPosition = $position * 2 + 1;
$addButtonCssSelector = "#edit-{$fieldSelector}-wrapper table > tbody > tr:nth-child({$addButtonPosition}) input.paragraphs-features__add-in-between__button";
}
$addButton = $page
->find('css', $addButtonCssSelector);
$this
->scrollElementInView($addButtonCssSelector);
$addButton
->click();
$this
->assertSession()
->assertWaitOnAjaxRequest();
$page
->find('xpath', "//div[contains(@class, \"ui-dialog-content\")]/*[contains(@class, \"paragraphs-add-dialog-list\")]//*[@name=\"{$fieldName}_{$type}_add_more\"]")
->click();
$this
->assertSession()
->assertWaitOnAjaxRequest();
static::assertEquals($this
->getNumberOfParagraphs($fieldName), $numberOfParagraphs + 1);
return $this
->getParagraphDelta($fieldName, $position);
}
public function getParagraphDelta($fieldName, $position) {
$fieldSelector = HTML::cleanCssIdentifier($fieldName);
$paragraphItem = $this
->getParagraphItems($fieldName)[$position];
$itemId = $paragraphItem
->getAttribute('id');
preg_match("/^edit-{$fieldSelector}-(\\d+)--/", $itemId, $matches);
if (!isset($matches[1])) {
throw new \Exception('No new paragraph is found');
}
return $matches[1];
}
public function addImageParagraph($fieldName, array $media, $position = NULL) {
$paragraphIndex = $this
->addParagraph($fieldName, 'image', $position);
$this
->selectMedia("{$fieldName}_{$paragraphIndex}_subform_field_image", 'image_browser', $media);
}
public function addVideoParagraph($fieldName, array $media, $position = NULL) {
$paragraphIndex = $this
->addParagraph($fieldName, 'video', $position);
$this
->selectMedia("{$fieldName}_{$paragraphIndex}_subform_field_video", 'video_browser', $media);
}
public function addGalleryParagraph($fieldName, $name, array $media, $position = NULL) {
$paragraphIndex = $this
->addParagraph($fieldName, 'gallery', $position);
$this
->createGallery($name, "{$fieldName}_{$paragraphIndex}_subform_field_media", $media);
}
public function addTextParagraph($fieldName, $text, $type = 'text', $position = NULL) {
$paragraphIndex = $this
->addParagraph($fieldName, $type, $position);
if (!empty($text)) {
$this
->fillCkEditor("textarea[name='{$fieldName}[{$paragraphIndex}][subform][field_text][0][value]']", $text);
}
}
public function addSocialParagraph($fieldName, $socialUrl, $type, $position = NULL) {
$paragraphIndex = $this
->addParagraph($fieldName, $type, $position);
$page = $this
->getSession()
->getPage();
if ($page
->hasField("{$fieldName}[{$paragraphIndex}][subform][field_media][0][inline_entity_form][field_url][0][value]")) {
$page
->fillField("{$fieldName}[{$paragraphIndex}][subform][field_media][0][inline_entity_form][field_url][0][value]", $socialUrl);
}
elseif ($page
->hasField("{$fieldName}[{$paragraphIndex}][subform][field_media][0][inline_entity_form][field_url][0][uri]")) {
$page
->fillField("{$fieldName}[{$paragraphIndex}][subform][field_media][0][inline_entity_form][field_url][0][uri]", $socialUrl);
}
}
public function addLinkParagraph($fieldName, $urlText, $url, $position = NULL) {
$paragraphIndex = $this
->addParagraph($fieldName, 'link', $position);
$page = $this
->getSession()
->getPage();
$page
->fillField("{$fieldName}[{$paragraphIndex}][subform][field_link][0][title]", $urlText);
$page
->fillField("{$fieldName}[{$paragraphIndex}][subform][field_link][0][uri]", $url);
}
public function editParagraph(DocumentElement $page, $paragraphsFieldName, $index) {
$editButtonName = "{$paragraphsFieldName}_{$index}_edit";
$this
->scrollElementInView("[name=\"{$editButtonName}\"]");
$page
->pressButton($editButtonName);
$this
->assertSession()
->assertWaitOnAjaxRequest();
}
}