View source
<?php
namespace Drupal\Tests\acquia_contenthub\Functional;
use Drupal\Component\Serialization\Json;
use Drupal\Component\Uuid\Uuid;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Tests\paragraphs\Functional\Experimental\ParagraphsExperimentalTestBase;
class ParagraphsTest extends ParagraphsExperimentalTestBase {
use StringTranslationTrait;
protected $adminUserPermissions = [
'administer nodes',
'bypass node access',
'administer acquia content hub',
'access administration pages',
];
protected $adminUser;
public static $modules = [
'node',
'paragraphs',
'acquia_contenthub',
'ch_node_with_paragraphs',
];
protected $node;
public function setUp() : void {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser($this->adminUserPermissions);
}
protected function setContentHubEntitySettings() {
$this
->drupalGet('admin/config/services/acquia-contenthub/configuration');
$edit = [
'entities[node][ch_node_with_paragraphs][enable_index]' => 1,
'entities[node][ch_node_with_paragraphs][enable_viewmodes]' => 1,
'entities[paragraph][addresses][enable_index]' => 1,
'entities[paragraph][client_data][enable_index]' => 1,
];
$this
->drupalPostForm(NULL, $edit, $this
->t('Save configuration'));
$this
->assertResponse(200);
}
public function testParagraphsCdf() {
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('node/add/ch_node_with_paragraphs');
$this
->getSession()
->getPage()
->findButton('field_client_data_client_data_add_more')
->press();
$this
->getSession()
->getPage()
->find('named', [
'name' => 'field_client_data_0_subform_field_client_location_addresses_add_more',
])
->press();
$this
->getSession()
->getPage()
->find('named', [
'name' => 'field_client_data_0_subform_field_client_location_addresses_add_more',
])
->press();
$title = 'Page_title';
$edit = [
'title[0][value]' => $title,
'field_client_data[0][subform][field_client_name][0][value]' => 'Client Name',
'field_client_data[0][subform][field_client_tags][0][target_id]' => 'test_term',
'field_client_data[0][subform][field_client_email][0][value]' => 'test@test.com',
'field_client_data[0][subform][field_client_location][0][subform][field_address_type]' => 'Work Address',
'field_client_data[0][subform][field_client_location][0][subform][field_address][0][value]' => 'Test Address',
'field_client_data[0][subform][field_client_location][1][subform][field_address_type]' => 'Shipping Address',
'field_client_data[0][subform][field_client_location][1][subform][field_address][0][value]' => 'Another Test Address',
];
if (floatval(\Drupal::VERSION) <= 8.300000000000001) {
$this
->submitForm($edit, $this
->t('Save and publish'));
}
else {
$this
->submitForm($edit, $this
->t('Save'));
}
$this->node = $this
->getNodeByTitle($title);
$this
->setContentHubEntitySettings();
$this
->checkCdfOutput();
}
public function checkCdfOutput() {
$output = $this
->drupalGetCdf('acquia-contenthub-cdf/' . $this->node
->getEntityTypeId() . '/' . $this->node
->id());
$this
->assertResponse(200);
$paragraphs = $this->node
->get('field_client_data')
->getValue();
$paragraphs_id_0 = $paragraphs[0]['target_id'];
$paragraphs_uuid_0 = $output['entities']['0']['attributes']['field_client_data']['value']['en']['0'];
$this
->assertTrue(isset($output['entities']['0']['metadata']), 'Metadata is present');
$this
->assertTrue(isset($output['entities']['0']['metadata']['view_modes']['default']), $this
->t('View mode %view_mode is present', [
'%view_mode' => 'default',
]));
$this
->assertEqual($output['entities']['0']['attributes']['field_client_data']['type'], 'array<reference>');
$this
->assertTrue(Uuid::isValid($paragraphs_uuid_0));
$output = $this
->drupalGetCdf('acquia-contenthub-cdf/entity/paragraph/' . $paragraphs_id_0, [
'query' => [
'entity_type' => 'paragraph',
'entity_id' => $paragraphs_id_0,
],
]);
$this
->assertResponse(200);
$client_data = \Drupal::entityTypeManager()
->getStorage("paragraph")
->load($paragraphs_id_0);
$field_client_location = $client_data
->get('field_client_location')
->getValue();
$paragraphs_id_1 = $field_client_location[0]['target_id'];
$paragraphs_id_2 = $field_client_location[1]['target_id'];
$paragraphs_uuid_1 = $output['entities']['0']['attributes']['field_client_location']['value']['en']['0'];
$paragraphs_uuid_2 = $output['entities']['0']['attributes']['field_client_location']['value']['en']['1'];
$this
->assertEqual($output['entities']['0']['uuid'], $paragraphs_uuid_0);
$this
->assertEqual($output['entities']['0']['type'], 'paragraph');
$this
->assertEqual($output['entities']['0']['attributes']['type']['value']['en'], 'client_data');
$this
->assertEqual($output['entities']['0']['attributes']['parent_type']['type'], 'array<string>');
$this
->assertEqual($output['entities']['0']['attributes']['parent_type']['value']['en']['0'], 'node');
$this
->assertEqual($output['entities']['0']['attributes']['parent_uuid']['type'], 'string');
$this
->assertEqual($output['entities']['0']['attributes']['parent_uuid']['value']['en'], $this->node
->uuid());
$this
->assertEqual($output['entities']['0']['attributes']['parent_field_name']['value']['en']['0'], 'field_client_data');
$this
->assertEqual($output['entities']['0']['attributes']['field_client_email']['value']['en']['0'], 'test@test.com');
$this
->assertEqual($output['entities']['0']['attributes']['field_client_name']['value']['en']['0'], 'Client Name');
$this
->assertTrue(Uuid::isValid($output['entities']['0']['attributes']['field_client_tags']['value']['en']['0']));
$this
->assertTrue(Uuid::isValid($paragraphs_uuid_1));
$this
->assertTrue(Uuid::isValid($paragraphs_uuid_2));
$output = $this
->drupalGetCdf('acquia-contenthub-cdf/entity/paragraph/' . $paragraphs_id_1, [
'query' => [
'entity_type' => 'paragraph',
'entity_id' => $paragraphs_id_1,
],
]);
$this
->assertResponse(200);
$this
->assertEqual($output['entities']['0']['uuid'], $paragraphs_uuid_1);
$this
->assertEqual($output['entities']['0']['type'], 'paragraph');
$this
->assertEqual($output['entities']['0']['attributes']['type']['value']['en'], 'addresses');
$this
->assertEqual($output['entities']['0']['attributes']['parent_uuid']['type'], 'string');
$this
->assertEqual($output['entities']['0']['attributes']['parent_uuid']['value']['en'], $paragraphs_uuid_0);
$this
->assertEqual($output['entities']['0']['attributes']['parent_type']['value']['en']['0'], 'paragraph');
$this
->assertEqual($output['entities']['0']['attributes']['parent_type']['type'], 'array<string>');
$this
->assertEqual($output['entities']['0']['attributes']['parent_field_name']['value']['en']['0'], 'field_client_location');
$this
->assertEqual($output['entities']['0']['attributes']['field_address']['value']['en']['0'], 'Test Address');
$this
->assertEqual($output['entities']['0']['attributes']['field_address_type']['value']['en']['0'], 'Work Address');
$output = $this
->drupalGetCdf('acquia-contenthub-cdf/entity/paragraph/' . $paragraphs_id_2, [
'query' => [
'entity_type' => 'paragraph',
'entity_id' => $paragraphs_id_2,
],
]);
$this
->assertResponse(200);
$this
->assertEqual($output['entities']['0']['uuid'], $paragraphs_uuid_2);
$this
->assertEqual($output['entities']['0']['type'], 'paragraph');
$this
->assertEqual($output['entities']['0']['attributes']['type']['value']['en'], 'addresses');
$this
->assertEqual($output['entities']['0']['attributes']['parent_uuid']['type'], 'string');
$this
->assertEqual($output['entities']['0']['attributes']['parent_uuid']['value']['en'], $paragraphs_uuid_0);
$this
->assertEqual($output['entities']['0']['attributes']['parent_type']['value']['en']['0'], 'paragraph');
$this
->assertEqual($output['entities']['0']['attributes']['parent_type']['type'], 'array<string>');
$this
->assertEqual($output['entities']['0']['attributes']['parent_field_name']['value']['en']['0'], 'field_client_location');
$this
->assertEqual($output['entities']['0']['attributes']['field_address']['value']['en']['0'], 'Another Test Address');
$this
->assertEqual($output['entities']['0']['attributes']['field_address_type']['value']['en']['0'], 'Shipping Address');
}
protected function drupalGetCdf($path, array $options = [], array $headers = []) {
return Json::decode($this
->drupalGetWithFormat($path, 'acquia_contenthub_cdf', $options, $headers));
}
}