3 namespace Drupal\Tests\blazy\Unit;
5 use Drupal\Tests\UnitTestCase;
6 use Drupal\Tests\blazy\Traits\BlazyUnitTestTrait;
7 use Drupal\Tests\blazy\Traits\BlazyManagerUnitTestTrait;
10 * @coversDefaultClass \Drupal\blazy\BlazyManager
14 class BlazyManagerUnitTest extends UnitTestCase {
16 use BlazyUnitTestTrait;
17 use BlazyManagerUnitTestTrait;
22 protected function setUp() {
25 $this->setUpUnitServices();
26 $this->setUpUnitContainer();
27 $this->setUpUnitImages();
29 $this->blazyManager->setLightboxes('blazy_test');
33 * Tests cases for various methods.
35 * @covers ::getEntityTypeManager
36 * @covers ::getModuleHandler
37 * @covers ::getRenderer
39 * @covers ::getConfigFactory
41 public function testBlazyManagerServiceInstances() {
42 $this->assertInstanceOf('\Drupal\Core\Entity\EntityTypeManagerInterface', $this->blazyManager->getEntityTypeManager());
43 $this->assertInstanceOf('\Drupal\Core\Extension\ModuleHandlerInterface', $this->blazyManager->getModuleHandler());
44 $this->assertInstanceOf('\Drupal\Core\Render\RendererInterface', $this->blazyManager->getRenderer());
45 $this->assertInstanceOf('\Drupal\Core\Config\ConfigFactoryInterface', $this->blazyManager->getConfigFactory());
46 $this->assertInstanceOf('\Drupal\Core\Cache\CacheBackendInterface', $this->blazyManager->getCache());
50 * Tests cases for config.
52 * @covers ::configLoad
54 public function testConfigLoad() {
55 $blazy = $this->blazyManager->configLoad('blazy');
56 $this->assertArrayHasKey('loadInvisible', $blazy);
58 $admin_css = $this->blazyManager->configLoad('admin_css', 'blazy.settings');
59 $this->assertTrue($admin_css, 'Blazy admin CSS is enabled by default.');
61 $responsive_image = $this->blazyManager->configLoad('responsive_image');
62 $this->assertTrue($responsive_image, 'Responsive image was disabled by default, yet enabled now.');
66 * Tests cases for config.
68 * @covers ::entityLoad
69 * @covers ::entityLoadMultiple
71 public function testEntityLoadImageStyle() {
72 $styles = $this->setUpImageStyle();
74 $ids = array_keys($styles);
75 $multiple = $this->blazyManager->entityLoadMultiple('image_style', $ids);
76 $this->assertArrayHasKey('large', $multiple);
78 $expected = $this->blazyManager->entityLoad('large', 'image_style');
79 $this->assertEquals($expected, $multiple['large']);
83 * Tests cases for config.
85 * @covers ::entityLoad
86 * @covers ::entityLoadMultiple
88 public function testEntityLoadResponsiveImageStyle() {
89 $styles = $this->setUpResponsiveImageStyle();
91 $ids = array_keys($styles);
92 $multiple = $this->blazyManager->entityLoadMultiple('responsive_image_style', $ids);
93 $this->assertArrayHasKey('blazy_picture_test', $multiple);
95 $expected = $this->blazyManager->entityLoad('blazy_picture_test', 'responsive_image_style');
96 $this->assertEquals($expected, $multiple['blazy_picture_test']);
100 * Test \Drupal\blazy\BlazyManager::cleanUpBreakpoints().
102 * @covers ::cleanUpBreakpoints
103 * @dataProvider providerTestCleanUpBreakpoints
105 public function testCleanUpBreakpoints($breakpoints, $expected_breakpoints, $blazy, $expected_blazy) {
106 $settings['blazy'] = $blazy;
107 $settings['breakpoints'] = $breakpoints;
109 $this->blazyManager->cleanUpBreakpoints($settings);
110 $this->assertEquals($expected_breakpoints, $settings['breakpoints']);
112 // Verify that Blazy is activated by breakpoints.
113 $this->assertEquals($expected_blazy, $settings['blazy']);
117 * Provider for ::testCleanUpBreakpoints().
119 public function providerTestCleanUpBreakpoints() {
128 $this->getEmptyBreakpoints(),
134 $this->getDataBreakpoints(),
135 $this->getDataBreakpoints(TRUE),
139 'mixed empty blazy enabled first' => [
140 $this->getDataBreakpoints(),
141 $this->getDataBreakpoints(TRUE),
149 * Tests for \Drupal\blazy\BlazyManager::preRenderImage().
152 * @covers ::preRenderImage
153 * @dataProvider providerTestPreRenderImage
155 public function testPreRenderImage($item, $uri, $content, $expected_image, $expected_render) {
158 $build['item'] = $item ? $this->testItem : [];
159 $build['content'] = $content;
160 $build['settings']['uri'] = $uri;
163 $build['item']->_attributes['data-blazy-test'] = TRUE;
166 $image = $this->blazyManager->getImage($build);
168 $build_image['#build']['settings'] = array_merge($this->getCacheMetaData(), $build['settings']);
169 $build_image['#build']['item'] = $build['item'];
171 $pre_render = $this->blazyManager->preRenderImage($build_image);
173 $check_image = !$expected_image ? empty($image) : !empty($image);
174 $this->assertTrue($check_image);
176 $check_pre_render = !$expected_render ? TRUE : !empty($pre_render);
177 $this->assertTrue($check_pre_render);
181 * Provide test cases for ::testPreRenderImage().
184 * An array of tested data.
186 public function providerTestPreRenderImage() {
203 'core/misc/druplicon.png',
210 'core/misc/druplicon.png',
211 '<iframe src="//www.youtube.com/watch?v=E03HFA923kw" class="b-lazy"></iframe>',
220 * Tests cases for attachments.
223 * @depends testConfigLoad
225 public function testAttach() {
230 'media_switch' => 'media',
235 $attachments = $this->blazyManager->attach($attach);
237 $this->assertArrayHasKey('library', $attachments);
238 $this->assertArrayHasKey('blazy', $attachments['drupalSettings']);
240 $this->assertContains('blazy/media', $attachments['library']);
241 $this->assertContains('blazy/ratio', $attachments['library']);
245 * Tests cases for lightboxes.
247 * @covers ::getLightboxes
248 * @covers ::setLightboxes
250 public function testGetLightboxes() {
251 $lightboxes = $this->blazyManager->getLightboxes();
253 $this->assertNotContains('nixbox', $lightboxes);
258 namespace Drupal\blazy;
260 if (!function_exists('blazy_test_theme')) {
265 function blazy_test_theme() {
266 // Empty block to satisfy coder.
271 if (!function_exists('colorbox_theme')) {
276 function colorbox_theme() {
277 // Empty block to satisfy coder.
282 if (!function_exists('photobox_theme')) {
287 function photobox_theme() {
288 // Empty block to satisfy coder.