View source
<?php
namespace Drupal\Tests\juicebox\Functional;
use Drupal\file\Entity\File;
use Drupal\Component\Utility\Html;
class JuiceboxFileCase extends JuiceboxCaseTestBase {
public static $modules = [
'node',
'field_ui',
'image',
'juicebox',
];
protected $instFieldName = 'field_file';
public $instFieldType = 'file';
public function setUp() {
parent::setUp();
$this->webUser = $this
->drupalCreateUser([
'access content',
'access administration pages',
'administer site configuration',
'administer content types',
'administer nodes',
'administer node fields',
'administer node display',
'bypass node access',
]);
$this
->drupalLogin($this->webUser);
$this
->initNode();
$this
->activateJuiceboxFieldFormatter();
}
public function testFile() {
$this
->createNodeWithFile();
$node = $this->node;
$xml_path = 'juicebox/xml/field/node/' . $node
->id() . '/' . $this->instFieldName . '/full';
$xml_url = \Drupal::url('juicebox.xml_field', [
'entityType' => 'node',
'entityId' => $node
->id(),
'fieldName' => $this->instFieldName,
'displayName' => 'full',
]);
$uri = File::load($node->{$this->instFieldName}[0]->target_id)
->getFileUri();
$test_image_url = entity_load('image_style', 'juicebox_medium')
->buildUrl($uri);
$test_thumb_url = entity_load('image_style', 'juicebox_square_thumb')
->buildUrl($uri);
$this
->drupalLogout();
$this
->drupalGet('node/' . $node
->id());
$this
->assertRaw(trim(json_encode([
'configUrl' => $xml_url,
]), '{}"'), 'Gallery setting found in Drupal.settings.');
$this
->assertRaw('id="node--' . $node
->id() . '--' . str_replace('_', '-', $this->instFieldName) . '--full"', 'Embed code wrapper found.');
$this
->assertRaw(Html::escape(file_url_transform_relative($test_image_url)), 'Test image found in embed code');
$this
->drupalGet($xml_path);
$this
->assertRaw('<?xml version="1.0" encoding="UTF-8"?>', 'Valid XML detected.');
$this
->assertRaw('imageURL="' . Html::escape($test_image_url), 'Test image found in XML.');
$this
->assertRaw('thumbURL="' . Html::escape($test_thumb_url), 'Test thumbnail found in XML.');
$this
->assertRaw('<juicebox gallerywidth="100%" galleryheight="100%" backgroundcolor="#222222" textcolor="rgba(255,255,255,1)" thumbframecolor="rgba(255,255,255,.5)" showopenbutton="TRUE" showexpandbutton="TRUE" showthumbsbutton="TRUE" usethumbdots="FALSE" usefullscreenexpand="FALSE">', 'Expected default configuration options set in XML.');
}
public function testFileNonImage() {
$this
->createNodeWithFile('text');
$node = $this->node;
$this
->drupalLogout();
$this
->drupalGet('juicebox/xml/field/node/' . $node
->id() . '/' . $this->instFieldName . '/full');
$this
->assertPattern('|imageURL=.*text.png.*thumbURL=.*text.png|', 'Non-image mimetype placeholder found for image and thumbnail.');
$this
->drupalLogin($this->webUser);
$this
->drupalGet('admin/structure/types/manage/' . $this->instBundle . '/display');
$this
->submitForm([], $this->instFieldName . '_settings_edit', 'entity-view-display-edit-form');
$edit = [
'fields[' . $this->instFieldName . '][settings_edit_form][settings][incompatible_file_action]' => 'skip',
];
$this
->submitForm($edit, 'Save');
$savemsg = $this
->t('Gallery configuration changes saved.');
$this
->assertText($savemsg);
$this
->drupalLogout();
$this
->drupalGet('juicebox/xml/field/node/' . $node
->id() . '/' . $this->instFieldName . '/full');
$this
->assertRaw('<?xml version="1.0" encoding="UTF-8"?>', 'Valid XML detected.');
$this
->assertNoRaw('<image', 'Non-image items was skipped.');
}
}