View source
<?php
namespace Drupal\Tests\juicebox\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Tests\TestFileCreationTrait;
abstract class JuiceboxCaseTestBase extends BrowserTestBase {
use TestFileCreationTrait;
protected $webUser;
protected $node;
protected $instBundle = 'juicebox_gallery';
protected $instFieldName = 'field_juicebox_image';
protected $instFieldType = 'image';
protected function initNode() {
$this
->drupalCreateContentType([
'type' => $this->instBundle,
'name' => $this->instBundle,
]);
$field_storage_settings = [
'display_field' => TRUE,
'display_default' => TRUE,
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
];
$field_storage = [
'entity_type' => 'node',
'field_name' => $this->instFieldName,
'type' => $this->instFieldType,
'settings' => $field_storage_settings,
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
];
entity_create('field_storage_config', $field_storage)
->save();
$field_settings = [];
if ($this->instFieldType == 'image') {
$field_settings['alt_field'] = TRUE;
$field_settings['alt_field_required'] = FALSE;
$field_settings['title_field'] = TRUE;
$field_settings['title_field_required'] = FALSE;
}
if ($this->instFieldType == 'file') {
$field_settings['description_field'] = TRUE;
$field_settings['file_extensions'] = 'txt jpg png mp3 rtf docx pdf';
}
$field = [
'field_name' => $this->instFieldName,
'label' => $this
->randomString(),
'entity_type' => 'node',
'bundle' => $this->instBundle,
'required' => FALSE,
'settings' => $field_settings,
];
entity_create('field_config', $field)
->save();
entity_get_form_display('node', $this->instBundle, 'default')
->setComponent($this->instFieldName, [
'type' => 'file_generic',
'settings' => [],
])
->save();
$entity_manager = $this->container
->get('entity.manager');
$entity_manager
->getStorage('field_storage_config')
->resetCache();
$entity_manager
->getStorage('field_config')
->resetCache();
}
protected function activateJuiceboxFieldFormatter() {
entity_get_display('node', $this->instBundle, 'default')
->setComponent($this->instFieldName, [
'type' => 'juicebox_formatter',
'settings' => [],
])
->save();
}
protected function createNodeWithFile($file_type = 'image', $multivalue = TRUE, $add_title_caption = TRUE) {
$file = current($this
->getTestFiles($file_type));
$edit = [
'title[0][value]' => 'Test Juicebox Gallery Node',
'files[' . $this->instFieldName . '_0]' . ($multivalue ? '[]' : '') => \Drupal::service('file_system')
->realpath($file->uri),
];
$this
->drupalGet('node/add/' . $this->instBundle);
$this
->submitForm($edit, 'Save');
$matches = [];
preg_match('/node\\/([0-9]+)/', $this
->getUrl(), $matches);
if (isset($matches[1])) {
$nid = $matches[1];
$edit = [
'body[0][value]' => 'Some body content on node ' . $nid . ' <strong>with formatting</strong>',
];
if ($add_title_caption) {
if ($this->instFieldType == 'image') {
$edit[$this->instFieldName . '[0][title]'] = 'Some title text for field ' . $this->instFieldName . ' on node ' . $nid;
$edit[$this->instFieldName . '[0][alt]'] = 'Some alt text for field ' . $this->instFieldName . ' on node ' . $nid . ' <strong>with formatting</strong>';
}
if ($this->instFieldType == 'file') {
$edit[$this->instFieldName . '[0][description]'] = 'Some description text for field ' . $this->instFieldName . ' on node ' . $nid . ' <strong>with formatting</strong>';
}
}
$this
->drupalGet('node/' . $nid . '/edit');
$this
->submitForm($edit, 'Save');
$node_storage = $this->container
->get('entity.manager')
->getStorage('node');
$node_storage
->resetCache([
$nid,
]);
$this->node = $node_storage
->load($nid);
return TRUE;
}
return FALSE;
}
protected function renderContextualLinks(array $ids, $current_path) {
$post = [];
for ($i = 0; $i < count($ids); $i++) {
$post['ids[' . $i . ']'] = $ids[$i];
}
return $this
->drupalPost('contextual/render', 'application/json', $post, [
'query' => [
'destination' => $current_path,
],
]);
}
}