/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); $this->fileSystem = $this->container->get('file_system'); $this->config('system.file')->set('default_scheme', 'public')->save(); $this->imageStyle = ImageStyle::create(['name' => 'test']); $this->imageStyle->save(); }
/** * Tests the dependency between ImageStyle and entity display components. */ public function testEntityDisplayDependency() { // Create two image styles. /** @var \Drupal\image\ImageStyleInterface $style */ $style = ImageStyle::create(['name' => 'main_style']); $style->save(); /** @var \Drupal\image\ImageStyleInterface $replacement */ $replacement = ImageStyle::create(['name' => 'replacement_style']); $replacement->save(); // Create a node-type, named 'note'. $node_type = NodeType::create(['type' => 'note']); $node_type->save(); // Create an image field and attach it to the 'note' node-type. FieldStorageConfig::create(['entity_type' => 'node', 'field_name' => 'sticker', 'type' => 'image'])->save(); FieldConfig::create(['entity_type' => 'node', 'field_name' => 'sticker', 'bundle' => 'note'])->save(); // Create the default entity view display and set the 'sticker' field to use // the 'main_style' images style in formatter. /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $view_display */ $view_display = EntityViewDisplay::create(['targetEntityType' => 'node', 'bundle' => 'note', 'mode' => 'default', 'status' => TRUE])->setComponent('sticker', ['settings' => ['image_style' => 'main_style']]); $view_display->save(); // Create the default entity form display and set the 'sticker' field to use // the 'main_style' images style in the widget. /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */ $form_display = EntityFormDisplay::create(['targetEntityType' => 'node', 'bundle' => 'note', 'mode' => 'default', 'status' => TRUE])->setComponent('sticker', ['settings' => ['preview_image_style' => 'main_style']]); $form_display->save(); // Check that the entity displays exists before dependency removal. $this->assertNotNull(EntityViewDisplay::load($view_display->id())); $this->assertNotNull(EntityFormDisplay::load($form_display->id())); // Delete the 'main_style' image style. Before that, emulate the UI process // of selecting a replacement style by setting the replacement image style // ID in the image style storage. /** @var \Drupal\image\ImageStyleStorageInterface $storage */ $storage = $this->container->get('entity.manager')->getStorage($style->getEntityTypeId()); $storage->setReplacementId('main_style', 'replacement_style'); $style->delete(); // Check that the entity displays exists after dependency removal. $this->assertNotNull($view_display = EntityViewDisplay::load($view_display->id())); $this->assertNotNull($form_display = EntityFormDisplay::load($form_display->id())); // Check that the 'sticker' formatter component exists in both displays. $this->assertNotNull($formatter = $view_display->getComponent('sticker')); $this->assertNotNull($widget = $form_display->getComponent('sticker')); // Check that both displays are using now 'replacement_style' for images. $this->assertSame('replacement_style', $formatter['settings']['image_style']); $this->assertSame('replacement_style', $widget['settings']['preview_image_style']); // Delete the 'replacement_style' without setting a replacement image style. $replacement->delete(); // The entity view and form displays exists after dependency removal. $this->assertNotNull($view_display = EntityViewDisplay::load($view_display->id())); $this->assertNotNull($form_display = EntityFormDisplay::load($form_display->id())); // The 'sticker' formatter component should be hidden in view display. $this->assertNull($view_display->getComponent('sticker')); $this->assertTrue($view_display->get('hidden')['sticker']); // The 'sticker' widget component should be active in form displays, but the // image preview should be disabled. $this->assertNotNull($widget = $form_display->getComponent('sticker')); $this->assertSame('', $widget['settings']['preview_image_style']); }
/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); $this->style = ImageStyle::create(['name' => 'style_foo', 'label' => $this->randomString()]); $this->style->save(); $this->replacementStyle = ImageStyle::create(['name' => 'style_bar', 'label' => $this->randomString()]); $this->replacementStyle->save(); $this->entityTypeManager = \Drupal::entityTypeManager(); }
/** * Tests importing image styles. */ public function testImport() { $style = ImageStyle::create(['name' => 'test']); $style->addImageEffect(['id' => 'image_module_test_null']); $style->addImageEffect(['id' => 'image_module_test_null']); $style->save(); $this->assertEqual(count($style->getEffects()), 2); $uuid = \Drupal::service('uuid')->generate(); $style->set('effects', [$uuid => ['id' => 'image_module_test_null']]); $style->save(); $style = ImageStyle::load('test'); $this->assertEqual(count($style->getEffects()), 1); }
/** * Tests devel silent. */ public function testDevelSilent() { // TODO Quickfix for dynamic_page_cache, enabled by default in the testing // profile. $this->container->get('module_installer')->uninstall(['dynamic_page_cache']); $web_user = $this->drupalCreateUser(['administer site configuration', 'access devel information', 'administer software updates']); $this->drupalLogin($web_user); // Ensure that devel is disabled if response come from routes that are // declared with '_devel_silent' requirement. $this->drupalGet('devel-silent/route-requirement'); $this->assertText(t('"_devel_silent" route requirement forces devel to be inactive.')); // Ensure that devel doesn't interfere with non html response (e.g JsonResponse). $response = $this->drupalGet('devel-silent/json'); $this->assertResponse(200); $expected = ['data' => 'Devel is active only on HtmlResponse.']; $this->assertIdentical(Json::decode($response), $expected); // Ensure that devel doesn't interfere with private image style creation // and with BinaryFileResponse response. $style = ImageStyle::create(['name' => 'zyx', 'label' => $this->randomString()]); $style->save(); $image = current($this->drupalGetTestFiles('image')); $image_uri = file_unmanaged_copy($image->uri, 'private://'); // Let the devel_test module know about this file, so it can claim // ownership in hook_file_download(). \Drupal::state()->set('devel.test_file_download', $image_uri); $this->drupalGet($style->buildUrl($image_uri)); $this->assertResponse(200); $this->assertRaw(file_get_contents($style->buildUri($image_uri)), 'URL returns expected file.'); // Ensure that devel doesn't interfere with private files and with // BinaryFileResponse response. $file = File::create(['uid' => $web_user->id(), 'filename' => 'drupal.txt', 'uri' => 'private://devel.txt', 'filemime' => 'text/plain', 'status' => FILE_STATUS_PERMANENT]); file_put_contents($file->getFileUri(), 'Hello world!'); $file->save(); // Let the image_module_test module know about this file, so it can claim // ownership in hook_file_download(). \Drupal::state()->set('devel.test_file_download', $file->getFileUri()); $this->drupalGet($file->url()); $this->assertResponse(200); $this->assertRaw(file_get_contents($file->getFileUri()), 'URL returns expected file.'); }
/** * Tests integration with image module. */ public function testImage() { /** @var \Drupal\image\ImageStyleInterface $style */ $style = ImageStyle::create(['name' => 'foo']); $style->save(); // Create a new image field 'bar' to be used in 'entity_test_fields' view. FieldStorageConfig::create(['entity_type' => 'entity_test', 'field_name' => 'bar', 'type' => 'image'])->save(); FieldConfig::create(['entity_type' => 'entity_test', 'bundle' => 'entity_test', 'field_name' => 'bar'])->save(); /** @var \Drupal\views\ViewEntityInterface $view */ $view = View::load('entity_test_fields'); $display =& $view->getDisplay('default'); // Add the 'bar' image field to 'entity_test_fields' view. $display['display_options']['fields']['bar'] = ['id' => 'bar', 'field' => 'bar', 'plugin_id' => 'field', 'table' => 'entity_test__bar', 'entity_type' => 'entity_test', 'entity_field' => 'bar', 'type' => 'image', 'settings' => ['image_style' => 'foo', 'image_link' => '']]; $view->save(); $dependencies = $view->getDependencies() + ['config' => []]; // Checks that style 'foo' is a dependency of view 'entity_test_fields'. $this->assertTrue(in_array('image.style.foo', $dependencies['config'])); // Delete the 'foo' image style. $style->delete(); // Checks that the view has been deleted too. $this->assertNull(View::load('entity_test_fields')); }
protected function setUp() { parent::setUp(); $this->style = ImageStyle::create(array('name' => 'style_foo', 'label' => $this->randomString())); $this->style->save(); }
/** * Test styled image dimensions cumulatively. */ function testImageDimensions() { $image_factory = $this->container->get('image.factory'); // Create a working copy of the file. $files = $this->drupalGetTestFiles('image'); $file = reset($files); $original_uri = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME); // Create a style. /** @var $style \Drupal\image\ImageStyleInterface */ $style = entity_create('image_style', array('name' => 'test', 'label' => 'Test')); $style->save(); $generated_uri = 'public://styles/test/public/' . \Drupal::service('file_system')->basename($original_uri); $url = $style->buildUrl($original_uri); $variables = array('#theme' => 'image_style', '#style_name' => 'test', '#uri' => $original_uri, '#width' => 40, '#height' => 20); // Verify that the original image matches the hard-coded values. $image_file = $image_factory->get($original_uri); $this->assertEqual($image_file->getWidth(), $variables['#width']); $this->assertEqual($image_file->getHeight(), $variables['#height']); // Scale an image that is wider than it is high. $effect = array('id' => 'image_scale', 'data' => array('width' => 120, 'height' => 90, 'upscale' => TRUE), 'weight' => 0); $style->addImageEffect($effect); $style->save(); $this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" width="120" height="60" alt="" class="image-style-test" />'); $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.'); $this->drupalGet($url); $this->assertResponse(200, 'Image was generated at the URL.'); $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.'); $image_file = $image_factory->get($generated_uri); $this->assertEqual($image_file->getWidth(), 120); $this->assertEqual($image_file->getHeight(), 60); // Rotate 90 degrees anticlockwise. $effect = array('id' => 'image_rotate', 'data' => array('degrees' => -90, 'random' => FALSE), 'weight' => 1); $style->addImageEffect($effect); $style->save(); $this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" width="60" height="120" alt="" class="image-style-test" />'); $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.'); $this->drupalGet($url); $this->assertResponse(200, 'Image was generated at the URL.'); $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.'); $image_file = $image_factory->get($generated_uri); $this->assertEqual($image_file->getWidth(), 60); $this->assertEqual($image_file->getHeight(), 120); // Scale an image that is higher than it is wide (rotated by previous effect). $effect = array('id' => 'image_scale', 'data' => array('width' => 120, 'height' => 90, 'upscale' => TRUE), 'weight' => 2); $style->addImageEffect($effect); $style->save(); $this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" width="45" height="90" alt="" class="image-style-test" />'); $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.'); $this->drupalGet($url); $this->assertResponse(200, 'Image was generated at the URL.'); $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.'); $image_file = $image_factory->get($generated_uri); $this->assertEqual($image_file->getWidth(), 45); $this->assertEqual($image_file->getHeight(), 90); // Test upscale disabled. $effect = array('id' => 'image_scale', 'data' => array('width' => 400, 'height' => 200, 'upscale' => FALSE), 'weight' => 3); $style->addImageEffect($effect); $style->save(); $this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" width="45" height="90" alt="" class="image-style-test" />'); $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.'); $this->drupalGet($url); $this->assertResponse(200, 'Image was generated at the URL.'); $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.'); $image_file = $image_factory->get($generated_uri); $this->assertEqual($image_file->getWidth(), 45); $this->assertEqual($image_file->getHeight(), 90); // Add a desaturate effect. $effect = array('id' => 'image_desaturate', 'data' => array(), 'weight' => 4); $style->addImageEffect($effect); $style->save(); $this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" width="45" height="90" alt="" class="image-style-test" />'); $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.'); $this->drupalGet($url); $this->assertResponse(200, 'Image was generated at the URL.'); $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.'); $image_file = $image_factory->get($generated_uri); $this->assertEqual($image_file->getWidth(), 45); $this->assertEqual($image_file->getHeight(), 90); // Add a random rotate effect. $effect = array('id' => 'image_rotate', 'data' => array('degrees' => 180, 'random' => TRUE), 'weight' => 5); $style->addImageEffect($effect); $style->save(); $this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" alt="" class="image-style-test" />'); $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.'); $this->drupalGet($url); $this->assertResponse(200, 'Image was generated at the URL.'); $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.'); // Add a crop effect. $effect = array('id' => 'image_crop', 'data' => array('width' => 30, 'height' => 30, 'anchor' => 'center-center'), 'weight' => 6); $style->addImageEffect($effect); $style->save(); $this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" width="30" height="30" alt="" class="image-style-test" />'); $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.'); $this->drupalGet($url); $this->assertResponse(200, 'Image was generated at the URL.'); $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.'); $image_file = $image_factory->get($generated_uri); $this->assertEqual($image_file->getWidth(), 30); $this->assertEqual($image_file->getHeight(), 30); // Rotate to a non-multiple of 90 degrees. $effect = array('id' => 'image_rotate', 'data' => array('degrees' => 57, 'random' => FALSE), 'weight' => 7); $effect_id = $style->addImageEffect($effect); $style->save(); $this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" alt="" class="image-style-test" />'); $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.'); $this->drupalGet($url); $this->assertResponse(200, 'Image was generated at the URL.'); $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.'); $effect_plugin = $style->getEffect($effect_id); $style->deleteImageEffect($effect_plugin); // Ensure that an effect can unset dimensions. $effect = array('id' => 'image_module_test_null', 'data' => array(), 'weight' => 8); $style->addImageEffect($effect); $style->save(); $this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" alt="" class="image-style-test" />'); // Test URI dependent image effect. $style = ImageStyle::create(['name' => 'test_uri', 'label' => 'Test URI']); $effect = ['id' => 'image_module_test_uri_dependent', 'data' => [], 'weight' => 0]; $style->addImageEffect($effect); $style->save(); $variables = ['#theme' => 'image_style', '#style_name' => 'test_uri', '#uri' => $original_uri, '#width' => 40, '#height' => 20]; // PNG original image. Should be resized to 100x100. $generated_uri = 'public://styles/test_uri/public/' . \Drupal::service('file_system')->basename($original_uri); $url = $style->buildUrl($original_uri); $this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" width="100" height="100" alt="" class="image-style-test-uri" />'); $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.'); $this->drupalGet($url); $this->assertResponse(200, 'Image was generated at the URL.'); $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.'); $image_file = $image_factory->get($generated_uri); $this->assertEqual($image_file->getWidth(), 100); $this->assertEqual($image_file->getHeight(), 100); // GIF original image. Should be resized to 50x50. $file = $files[1]; $original_uri = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME); $generated_uri = 'public://styles/test_uri/public/' . \Drupal::service('file_system')->basename($original_uri); $url = $style->buildUrl($original_uri); $variables['#uri'] = $original_uri; $this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" width="50" height="50" alt="" class="image-style-test-uri" />'); $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.'); $this->drupalGet($url); $this->assertResponse(200, 'Image was generated at the URL.'); $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.'); $image_file = $image_factory->get($generated_uri); $this->assertEqual($image_file->getWidth(), 50); $this->assertEqual($image_file->getHeight(), 50); }
/** * Tests devel silent. */ public function testDevelSilent() { // TODO Quickfix for dynamic_page_cache, enabled by default in the testing // profile. $this->container->get('module_installer')->uninstall(['dynamic_page_cache']); // Enable timer so we can test if devel_silent() works properly by checking // the output of the page. \Drupal::configFactory()->getEditable('devel.settings')->set('timer', TRUE)->save(); $web_user = $this->drupalCreateUser(['administer site configuration', 'access devel information', 'administer software updates']); $this->drupalLogin($web_user); $this->drupalGet(''); $this->assertText('Page execution time was'); // Ensure that devel is disabled if the request has XDEBUG_PROFILE // parameter set. Get the user profile page so we are sure that we are not // redirected and we don't lose the query string parameter. $this->drupalGet('user/' . $web_user->id(), ['query' => ['XDEBUG_PROFILE' => '1']]); $this->assertNoText('Page execution time was'); // TODO this assertion seems to break testbot, we need to investigate. // Ensure that devel is disabled if the request come from Apache Benchmark. // $this->drupalGet('', [], ['User-Agent: ApacheBench/1.0']); // $this->assertNoText('Page execution time was'); // Ensure that devel is disabled on the front controller update.php. $this->drupalGet(Url::fromRoute('system.db_update')); $this->assertResponse(200); $this->assertNoText('Page execution time was'); // Ensure that devel is disabled if $GLOBALS['devel_shutdown'] is set // somewhere in the code. $this->drupalGet('devel-silent/global-shoutdown'); $this->assertText(t('$GLOBALS[\'devel_shutdown\'] = FALSE forces devel to be inactive.')); $this->assertNoText('Page execution time was'); // Ensure that devel is disabled if response come from routes that are // declared with '_devel_silent' requirement. $this->drupalGet('devel-silent/route-requirement'); $this->assertText(t('"_devel_silent" route requirement forces devel to be inactive.')); $this->assertNoText('Page execution time was'); // Ensure that devel doesn't interfere with non html response (e.g JsonResponse). $response = $this->drupalGet('devel-silent/json'); $this->assertResponse(200); $expected = ['data' => 'Devel is active only on HtmlResponse.']; $this->assertIdentical(Json::decode($response), $expected); // Ensure that devel doesn't interfere with private image style creation // and with BinaryFileResponse response. $style = ImageStyle::create(['name' => 'zyx', 'label' => $this->randomString()]); $style->save(); $image = current($this->drupalGetTestFiles('image')); $image_uri = file_unmanaged_copy($image->uri, 'private://'); // Let the devel_test module know about this file, so it can claim // ownership in hook_file_download(). \Drupal::state()->set('devel.test_file_download', $image_uri); $this->drupalGet($style->buildUrl($image_uri)); $this->assertResponse(200); $this->assertRaw(file_get_contents($style->buildUri($image_uri)), 'URL returns expected file.'); $this->assertNoText('Page execution time was'); // Ensure that devel doesn't interfere with private files and with // BinaryFileResponse response. $file = File::create(['uid' => $web_user->id(), 'filename' => 'drupal.txt', 'uri' => 'private://devel.txt', 'filemime' => 'text/plain', 'status' => FILE_STATUS_PERMANENT]); file_put_contents($file->getFileUri(), 'Hello world!'); $file->save(); // Let the image_module_test module know about this file, so it can claim // ownership in hook_file_download(). \Drupal::state()->set('devel.test_file_download', $file->getFileUri()); $this->drupalGet($file->url()); $this->assertResponse(200); $this->assertRaw(file_get_contents($file->getFileUri()), 'URL returns expected file.'); $this->assertNoText('Page execution time was'); }
/** * Tests usage of the image style theme function. */ function testImageStyleTheme() { /** @var \Drupal\Core\Render\RendererInterface $renderer */ $renderer = $this->container->get('renderer'); // Create an image. $files = $this->drupalGetTestFiles('image'); $file = reset($files); $original_uri = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME); // Create a style. $style = ImageStyle::create(array('name' => 'image_test', 'label' => 'Test')); $style->save(); $url = file_url_transform_relative($style->buildUrl($original_uri)); // Create the base element that we'll use in the tests below. $base_element = array('#theme' => 'image_style', '#style_name' => 'image_test', '#uri' => $original_uri); $element = $base_element; $this->setRawContent($renderer->renderRoot($element)); $elements = $this->xpath('//img[@class="image-style-image-test" and @src=:url and @alt=""]', array(':url' => $url)); $this->assertEqual(count($elements), 1, 'theme_image_style() renders an image correctly.'); // Test using theme_image_style() with a NULL value for the alt option. $element = $base_element; $element['#alt'] = NULL; $this->setRawContent($renderer->renderRoot($element)); $elements = $this->xpath('//img[@class="image-style-image-test" and @src=:url]', array(':url' => $url)); $this->assertEqual(count($elements), 1, 'theme_image_style() renders an image correctly with a NULL value for the alt option.'); }
/** * Tests access for the image style listing. */ public function testImageStyleAccess() { $style = ImageStyle::create(array('name' => 'style_foo', 'label' => $this->randomString())); $style->save(); $this->drupalGet('admin/config/media/image-styles'); $this->clickLink(t('Edit')); $this->assertRaw(t('Select a new effect')); }