Commit b4873f35 authored by Sam152's avatar Sam152 Committed by frjo
Browse files

Issue #2719897 by Sam152: Take advantage of the new JavascriptTestBase

parent d159bc3c
......@@ -4,3 +4,6 @@ description: 'A light-weight, customizable lightbox plugin for jQuery.'
package: Media
core: 8.x
configure: colorbox.admin_settings
dependencies:
- image
......@@ -161,7 +161,7 @@ function template_preprocess_colorbox_formatter(&$variables) {
$file_obj = file_load($fid);
$file_array = $file_obj->toArray();
// populate the image title
if (Unicode::strlen($file_array['field_image_title_text'][0]['value']) != 0 && empty($item->title) && $settings['colorbox_caption'] == 'title') {
if (!empty($file_array['field_image_title_text'][0]['value']) && empty($item->title) && $settings['colorbox_caption'] == 'title') {
$caption = $file_array['field_image_title_text'][0]['value'];
}
// populate the image alt text.
......
<?php
/**
* @file
* Contains \Drupal\colorbox\Tests\Colorbox.
*/
namespace Drupal\colorbox\Tests;
use Drupal\image\Tests\ImageFieldTestBase;
/**
* Test the colorbox module.
*
* @group colorbox
*/
class Colorbox extends ImageFieldTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = [
'colorbox',
'field_test',
'node',
'field_ui',
'image'
];
/**
* The field name used in the test.
*
* @var string
*/
protected $fieldName = 'field_colorbox_test';
/**
* The content type used in the test.
*
* @var string
*/
protected $contentType = 'article';
/**
* The node ID with an uploaded image, ready for testing.
*
* @var int
*/
protected $testNid;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->createImageField($this->fieldName, $this->contentType, ['uri_scheme' => 'public'], ['alt_field_required' => 0]);
entity_get_display('node', $this->contentType, 'default')
->setComponent($this->fieldName, [
'type' => 'colorbox',
'settings' => [],
])
->save();
$this->drupalGet('node/add/article');
$test_image = current($this->drupalGetTestFiles('image'));
$this->testNid = $this->uploadNodeImage($test_image, $this->fieldName, 'article', '');
}
/**
* Test the colorbox formatter.
*/
public function testColorboxFormatter() {
$this->drupalGet('node/' . $this->testNid);
// Ensure all the relevant colorbox JavaScript is loaded on the page.
$this->assertScript('libraries/colorbox/jquery.colorbox-min.js');
$this->assertScript('modules/colorbox/styles/default/colorbox_style.js');
$this->assertScript('modules/colorbox/js/colorbox.js');
// Ensure the image appears with the relevant colorbox markup.
$this->assertRaw('rel="gallery-article-1-');
// The script should not appear by default on the homepage.
$this->drupalGet('<front>');
$this->assertScript('libraries/colorbox/jquery.colorbox-min.js', FALSE);
}
/**
* Assert a script is appearing in the document.
*
* @param string $script_url
* The script URL to assert
* @pararm bool $exists
* A flag to indicate if the script should exist or not exist for a pass.
*/
public function assertScript($script_url, $exists = TRUE) {
$version = \Drupal::VERSION;
$base_path = \Drupal::request()->getBasePath();
$this->{$exists ? 'assertRaw' : 'assertNoRaw'}("<script src=\"$base_path/$script_url?v=$version\"></script>");
}
}
name: Colorbox Library Test
description: Ensure colorbox can be tested properly with the downloaded library.
type: module
hidden: true
package: Testing
core: 8.x
<?php
/**
* @file
* Install file for the colorbox library test module.
*/
use Drupal\Core\Site\Settings;
/**
* Implements hook_install().
*/
function colorbox_library_test_install() {
$library_folder = Settings::get('file_public_path') . '/libraries';
$zip_file = $library_folder . '/colorbox.zip';
$colorbox_folder = $library_folder . '/colorbox';
if (file_exists($colorbox_folder)) {
return;
}
file_prepare_directory($library_folder, FILE_MODIFY_PERMISSIONS | FILE_CREATE_DIRECTORY);
copy('https://github.com/jackmoore/colorbox/archive/master.zip', $zip_file);
$zip = new \ZipArchive();
$zip->open($zip_file);
$zip->extractTo($library_folder);
rename($library_folder . '/colorbox-master', $colorbox_folder);
}
<?php
/**
* @file
* Test specific changes for the colorbox library test.
*/
use Drupal\Core\Site\Settings;
/**
* Implements hook_library_alter().
*/
function colorbox_library_test_library_info_alter(&$libraries, $extension) {
if ($extension !== 'colorbox') {
return;
}
foreach ($libraries['colorbox']['js'] as $key => $library) {
$libraries['colorbox']['js']['/' . Settings::get('file_public_path') . $key] = $library;
unset($libraries['colorbox']['js'][$key]);
}
}
<?php
namespace Drupal\Tests\colorbox\FunctionalJavascript;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\file\Entity\File;
use Drupal\FunctionalJavascriptTests\JavascriptTestBase;
use Drupal\simpletest\ContentTypeCreationTrait;
use Drupal\simpletest\NodeCreationTrait;
/**
* Test the colorbox JavaScript.
*
* @group colorbox
*/
class ColorboxJavascriptTest extends JavascriptTestBase {
use NodeCreationTrait;
use ContentTypeCreationTrait;
/**
* {@inheritdoc}
*/
public static $modules = [
'colorbox',
'colorbox_library_test',
'node',
];
/**
* How long to wait for colorbox to launch.
*/
const COLORBOX_WAIT_TIMEOUT = 500;
/**
* @var \Drupal\node\NodeInterface
*/
protected $node;
/**
* Test the colorbox launches when a gallery is clicked.
*/
public function testColorboxLaunches() {
$this->drupalGet('node/' . $this->node->id());
$this->getSession()->getPage()->find('css', 'img')->click();
$this->getSession()->wait(static::COLORBOX_WAIT_TIMEOUT);
$this->assertSession()->elementContains('css', '#colorbox', 'test.png');
}
/**
* Test the gallery works.
*/
public function testColorboxGallery() {
$this->drupalGet('node/' . $this->node->id());
// Click and launch the gallery.
$this->getSession()->getPage()->find('css', 'img')->click();
$this->getSession()->wait(static::COLORBOX_WAIT_TIMEOUT);
$this->assertSession()->elementContains('css', '#cboxTitle', 'Image title 1');
$this->assertSession()->elementContains('css', '#colorbox', 'test.png');
// Click on the next image and assert the second image is visible.
$this->assertSession()->elementExists('css', '#cboxNext')->click();
$this->getSession()->wait(static::COLORBOX_WAIT_TIMEOUT);
$this->assertSession()->elementContains('css', '#cboxTitle', 'Image title 2');
$this->assertSession()->elementContains('css', '#colorbox', 'test.png');
// Use alt captions.
entity_get_display('node', 'page', 'default')
->setComponent('field_test_image', [
'type' => 'colorbox',
'settings' => ['colorbox_caption' => 'alt'],
'status' => TRUE,
])
->save();
drupal_flush_all_caches();
// Ensure the caption being used is the alt text.
$this->drupalGet('node/' . $this->node->id());
$this->getSession()->getPage()->find('css', 'img')->click();
$this->getSession()->wait(static::COLORBOX_WAIT_TIMEOUT);
$this->assertSession()->elementContains('css', '#cboxTitle', 'Image alt 1');
}
/**
* Test the mobile detection
*/
public function testMobileDetection() {
$this->changeSetting('advanced.mobile_detect', TRUE);
$this->changeSetting('advanced.mobile_detect_width', '1200px');
$this->getSession()->resizeWindow(200, 200);
$this->drupalGet('node/' . $this->node->id());
$this->assertSession()->elementAttributeContains('css', '#colorbox', 'style', 'display: none;');
}
/**
* Test the admin form.
*/
public function testAdminForm() {
$this->drupalLogin($this->drupalCreateUser([
'administer site configuration',
]));
$this->drupalGet('admin/config/media/colorbox');
$this->assertFalse($this->getSession()->getPage()->find('css', '.form-item-colorbox-transition-speed')->isVisible());
$this->assertSession()->fieldExists('colorbox_custom_settings_activate')->setValue(TRUE);
$this->assertTrue($this->getSession()->getPage()->find('css', '.form-item-colorbox-transition-speed')->isVisible());
$this->assertSession()->fieldExists('colorbox_overlayclose')->setValue(FALSE);
$this->getSession()->getPage()->find('css', '.form-submit')->click();
$this->assertSession()->pageTextContains('The configuration options have been saved.');
}
/**
* Change a colorbox setting.
*
* @param string $setting
* The name of the setting.
* @param string $value
* The value.
*/
protected function changeSetting($setting, $value) {
\Drupal::configFactory()
->getEditable('colorbox.settings')
->set($setting, $value)
->save();
}
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->createContentType(['type' => 'page']);
FieldStorageConfig::create(array(
'field_name' => 'field_test_image',
'entity_type' => 'node',
'type' => 'image',
'settings' => [],
'cardinality' => 3,
))->save();
$field_config = FieldConfig::create([
'field_name' => 'field_test_image',
'label' => 'Colorbox Field',
'entity_type' => 'node',
'bundle' => 'page',
'required' => TRUE,
'settings' => [],
]);
$field_config->save();
entity_get_display('node', 'page', 'default')
->setComponent('field_test_image', [
'type' => 'colorbox',
'settings' => [],
'status' => TRUE,
])
->save();
file_unmanaged_copy(DRUPAL_ROOT . '/core/modules/simpletest/files/image-1.png', 'public://test.png');
$file_a = File::create([
'uri' => 'public://test.png',
'filename' => 'test.png',
]);
$file_a->save();
$file_b = File::create([
'uri' => 'public://test.png',
'filename' => 'test.png',
]);
$file_b->save();
$this->node = $this->createNode([
'type' => 'page',
'field_test_image' => [
[
'target_id' => $file_a->id(),
'alt' => 'Image alt 1',
'title' => 'Image title 1',
],
[
'target_id' => $file_b->id(),
'alt' => 'Image alt 2',
'title' => 'Image title 2',
]
],
]);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment