View source
<?php
namespace Drupal\Tests\feeds_para_mapper\Functional;
use Drupal\Tests\BrowserTestBase;
abstract class FeedsParaMapperTestBase extends BrowserTestBase {
protected $webUser;
protected $profile = 'testing';
protected $bundles;
protected $contentType;
protected $feedType;
protected $paragraphField;
protected $importer;
protected $multiValued = FALSE;
protected $multiValuedParagraph = FALSE;
protected $createdPlugins;
protected $nested = FALSE;
public static $modules = array(
'node',
'field',
'field_ui',
'image',
'paragraphs',
'feeds',
'feeds_para_mapper',
);
protected function setUp() {
$args = func_get_args();
if (isset($args[0])) {
$this->multiValued = $args[0];
}
if (isset($args[1])) {
$this->multiValuedParagraph = $args[1];
}
if (isset($args[2])) {
$this->nested = $args[2];
}
$permissions = array(
'bypass node access',
'administer nodes',
'administer feeds',
'administer feeds_feed fields',
'administer content types',
'administer paragraphs types',
'administer paragraph fields',
'administer node fields',
);
if ($this->multiValued || $this->multiValuedParagraph) {
self::$modules[] = "feeds_tamper";
self::$modules[] = "feeds_tamper_ui";
$permissions[] = "administer feeds_tamper";
}
parent::setUp();
$this->webUser = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($this->webUser);
$this
->createBundles();
$this->contentType = "product";
$this->paragraphField = "details";
$last_key = count($this->bundles) - 1;
$last_bundle = array(
$this->bundles[$last_key]['name'],
);
$this
->createCT($this->contentType, $this->paragraphField, $last_bundle);
$this->feedType = "product_feed";
$this
->createFeedType($this->contentType, $this->feedType);
}
protected function createBundles() {
$counter = $this->nested ? 2 : 1;
for ($i = 0; $i < $counter; $i++) {
if ($i === 0) {
$cardinality = $this->multiValued ? -1 : 1;
$bundle = array(
'name' => 'image_details_bundle',
'fields' => array(
array(
'name' => 'description',
'type' => "text",
'widget' => 'text_textfield',
'cardinality' => $cardinality,
'mapping' => array(
'text' => 'field_description',
),
'mapping_multiple' => array(
'text_multiple' => 'field_description',
),
),
array(
'name' => 'image',
'type' => "image",
'widget' => 'image_image',
'cardinality' => $cardinality,
'mapping' => array(
'image_alt' => 'field_image:alt',
'image_title' => 'field_image:title',
'image_uri' => 'field_image:uri',
),
'mapping_multiple' => array(
'image_multi_alt' => 'field_image:alt',
'image_multi_title' => 'field_image:title',
'image_multi_uri' => 'field_image:uri',
),
),
),
);
}
else {
$isLast = $i + 1 === $counter;
$cardinality = $this->multiValuedParagraph && $isLast ? -1 : 1;
$bundle = array(
'name' => 'image_bundle',
'fields' => array(
array(
'name' => 'images',
'type' => "paragraphs",
'widget' => 'paragraphs_embed',
'cardinality' => $cardinality,
'allowed_bundles' => array(
end($this->bundles)['name'],
),
),
),
);
}
$this->bundles[] = $bundle;
$this
->createBundle($bundle);
}
}
protected function createCT($name, $paragraph_field, array $allowed_bundles) {
parent::createContentType(array(
'type' => $name,
));
$fields = array();
$cardinality = $this->multiValuedParagraph ? -1 : 1;
$fields[$paragraph_field] = array(
'type' => "field_ui:entity_reference_revisions:paragraph",
'widget' => 'paragraphs_embed',
'cardinality' => $cardinality,
'bundles' => $allowed_bundles,
);
$path = "admin/structure/types/manage/{$name}/fields/add-field";
foreach ($fields as $field_name => $details) {
$this
->createField($path, $field_name, $details['cardinality'], $details['type'], $details['widget'], $details['bundles']);
}
}
protected function createFeedType($contentType, $feedType) {
$this
->drupalGet('admin/structure/feeds/add');
$edit = array(
'id' => $feedType,
'label' => $feedType,
'description' => $feedType,
'fetcher' => 'upload',
'parser' => 'csv',
'processor' => 'entity:node',
'processor_wrapper[advanced][values][type]' => $contentType,
);
$this
->drupalPostForm(null, $edit, t("Save and add mappings"));
}
protected function createBundle(array $bundle) {
$this
->drupalGet('admin/structure/paragraphs_type/add');
$edit = array(
'label' => $bundle['name'],
'id' => $bundle['name'],
);
$this
->drupalPostForm(NULL, $edit, t('Save and manage fields'));
$text = t("Saved the @name Paragraphs type.", array(
'@name' => $bundle['name'],
));
$this
->assertSession()
->pageTextContains($text);
$fields = $bundle['fields'];
foreach ($fields as $field) {
$allowed_bundles = array();
if (isset($field['allowed_bundles'])) {
$allowed_bundles = $field['allowed_bundles'];
}
$path = "admin/structure/paragraphs_type/{$bundle['name']}/fields/add-field";
$this
->createField($path, $field['name'], $field['cardinality'], $field['type'], $field['widget'], $allowed_bundles);
}
}
protected function createField($form_path, $field_name, $cardinality, $type, $widget, array $bundles = array()) {
$this
->drupalGet($form_path);
$edit = array(
'new_storage_type' => $type,
'label' => $field_name,
'field_name' => $field_name,
);
$this
->drupalPostForm(NULL, $edit, t('Save and continue'));
$edit = array(
'cardinality_number' => (string) $cardinality,
);
if ($cardinality === -1) {
$edit = array(
'cardinality' => "-1",
);
}
$this
->drupalPostForm(NULL, $edit, t('Save field settings'));
$edit = array();
if (isset($bundles) && count($bundles)) {
foreach ($bundles as $bundle) {
$edit['settings[handler_settings][target_bundles_drag_drop][' . $bundle . '][enabled]'] = '1';
}
}
$this
->drupalPostForm(NULL, $edit, t('Save settings'));
$this
->assertSession()
->pageTextContains(t('Saved @name configuration.', array(
'@name' => $field_name,
)));
}
}