View source
<?php
namespace Drupal\Tests\juicebox\Functional;
use Drupal\file\Entity\File;
use Drupal\Component\Utility\Html;
use Drupal\Tests\Traits\Core\CronRunTrait;
class JuiceboxFieldFormatterCase extends JuiceboxCaseTestBase {
use CronRunTrait;
public static $modules = [
'node',
'field_ui',
'image',
'juicebox',
'search',
'contextual',
];
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',
'search content',
'access contextual links',
]);
$this
->drupalLogin($this->webUser);
$this
->initNode();
$this
->activateJuiceboxFieldFormatter();
$this
->createNodeWithFile();
$this
->drupalLogout();
}
public function testFieldFormatterConf() {
$node = $this->node;
$this
->drupalGet('node/' . $node
->id());
$this
->assertResponse(200, 'Control request of test node was successful.');
$this
->drupalGet('juicebox/xml/field/node/' . $node
->id() . '/' . $this->instFieldName . '/full');
$this
->assertResponse(200, 'Control request of XML was successful.');
$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][image_style]' => '',
'fields[' . $this->instFieldName . '][settings_edit_form][settings][thumb_style]' => 'thumbnail',
'fields[' . $this->instFieldName . '][settings_edit_form][settings][caption_source]' => 'alt',
'fields[' . $this->instFieldName . '][settings_edit_form][settings][title_source]' => 'title',
];
$this
->submitForm($edit, 'Save');
$this
->assertText($this
->t('Your settings have been saved.'), 'Gallery configuration changes saved.');
$uri = File::load($node->{$this->instFieldName}[0]->target_id)
->getFileUri();
$test_formatted_image_url = file_create_url($uri);
$test_formatted_thumb_url = entity_load('image_style', 'thumbnail')
->buildUrl($uri);
$this
->drupalLogout();
$this
->drupalGet('node/' . $node
->id());
$this
->assertRaw(Html::escape(file_url_transform_relative($test_formatted_image_url)), 'Test styled image found in embed code');
$this
->drupalGet('juicebox/xml/field/node/' . $node
->id() . '/' . $this->instFieldName . '/full');
$this
->assertRaw('imageURL="' . Html::escape($test_formatted_image_url), 'Test styled image found in XML.');
$this
->assertRaw('thumbURL="' . Html::escape($test_formatted_thumb_url), 'Test styled thumbnail found in XML.');
$this
->assertRaw('<title><![CDATA[Some title text for field ' . $this->instFieldName . ' on node ' . $node
->id() . ']]></title>', 'Image title text found in XML');
$this
->assertRaw('<caption><![CDATA[Some alt text for field ' . $this->instFieldName . ' on node ' . $node
->id() . ' <strong>with formatting</strong>]]></caption>', 'Image caption text found in XML');
$this
->drupalLogin($this->webUser);
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->submitForm([], 'Save');
$this
->cronRun();
$this
->drupalGet('search');
$this
->submitForm([
'keys' => '"Some title text"',
], 'Search');
$this
->assertText('Test Juicebox Gallery Node', 'Juicebox node found in search for title text.');
$this
->assertNoRaw('"configUrl":"', 'Juicebox Drupal.settings vars not included on search result page.');
}
public function testFieldFormatterAccess() {
$node = $this->node;
$this
->drupalGet('node/' . $node
->id());
$this
->assertResponse(200, 'Access allowed for published node.');
$this
->drupalGet('juicebox/xml/field/node/' . $node
->id() . '/' . $this->instFieldName . '/full');
$this
->assertRaw('<?xml version="1.0" encoding="UTF-8"', 'XML access allowed to published node (valid XML detected).');
$node->status = NODE_NOT_PUBLISHED;
$node
->save();
$this
->drupalGet('node/' . $node
->id());
$this
->assertResponse(403, 'Access blocked for unpublished node.');
$this
->drupalGet('juicebox/xml/field/node/' . $node
->id() . '/' . $this->instFieldName . '/full');
$this
->assertResponse(403, 'XML access blocked for unpublished node.');
}
}