/** * Tests file field formatter Entity Embed Display plugins. */ public function testFileFieldFormatter() { // Ensure that file field formatters are available as plugins. $this->assertAvailableDisplayPlugins($this->file, ['entity_reference:entity_reference_label', 'entity_reference:entity_reference_entity_id', 'file:file_default', 'file:file_table', 'file:file_url_plain']); // Ensure that correct form attributes are returned for the file field // formatter plugins. $form = array(); $form_state = new FormState(); $plugins = array('file:file_table', 'file:file_default', 'file:file_url_plain'); // Ensure that description field is available for all the 'file' plugins. foreach ($plugins as $plugin) { $display = $this->displayPluginManager()->createInstance($plugin, array()); $display->setContextValue('entity', $this->file); $conf_form = $display->buildConfigurationForm($form, $form_state); $this->assertIdentical(array_keys($conf_form), array('description')); $this->assertIdentical($conf_form['description']['#type'], 'textfield'); $this->assertIdentical((string) $conf_form['description']['#title'], 'Description'); } // Test entity embed using 'Generic file' Entity Embed Display plugin. $embed_settings = array('description' => "This is sample description"); $content = '<drupal-entity data-entity-type="file" data-entity-uuid="' . $this->file->uuid() . '" data-entity-embed-display="file:file_default" data-entity-embed-settings=\'' . Json::encode($embed_settings) . '\'>This placeholder should not be rendered.</drupal-entity>'; $settings = array(); $settings['type'] = 'page'; $settings['title'] = 'Test entity embed with file:file_default'; $settings['body'] = array(array('value' => $content, 'format' => 'custom_format')); $node = $this->drupalCreateNode($settings); $this->drupalGet('node/' . $node->id()); $this->assertText($embed_settings['description'], 'Description of the embedded file exists in page.'); $this->assertNoText(strip_tags($content), 'Placeholder does not appears in the output when embed is successful.'); $this->assertLinkByHref(file_create_url($this->file->getFileUri()), 0, 'Link to the embedded file exists.'); }
/** * Tests if file fields in teasers have correct resources. * * Ensure that file fields have the correct resource as the object in RDFa * when displayed as a teaser. */ function testNodeTeaser() { // Render the teaser. $node_render_array = entity_view_multiple(array($this->node), 'teaser'); $html = \Drupal::service('renderer')->renderRoot($node_render_array); // Parses front page where the node is displayed in its teaser form. $parser = new \EasyRdf_Parser_Rdfa(); $graph = new \EasyRdf_Graph(); $base_uri = \Drupal::url('<front>', [], ['absolute' => TRUE]); $parser->parse($graph, $html, 'rdfa', $base_uri); $node_uri = $this->node->url('canonical', ['absolute' => TRUE]); $file_uri = file_create_url($this->file->getFileUri()); // Node relation to attached file. $expected_value = array('type' => 'uri', 'value' => $file_uri); $this->assertTrue($graph->hasProperty($node_uri, 'http://www.w3.org/2000/01/rdf-schema#seeAlso', $expected_value), 'Node to file relation found in RDF output (rdfs:seeAlso).'); $this->drupalGet('node'); }
/** * Tests image field formatter Entity Embed Display plugin. */ public function testImageFieldFormatter() { // Ensure that image field formatters are available as plugins. $this->assertAvailableDisplayPlugins($this->image, ['entity_reference:entity_reference_label', 'entity_reference:entity_reference_entity_id', 'file:file_default', 'file:file_table', 'file:file_url_plain', 'image:responsive_image', 'image:image']); // Ensure that correct form attributes are returned for the image plugin. $form = array(); $form_state = new FormState(); $display = $this->displayPluginManager()->createInstance('image:image', array()); $display->setContextValue('entity', $this->image); $conf_form = $display->buildConfigurationForm($form, $form_state); $this->assertIdentical(array_keys($conf_form), array('image_style', 'image_link', 'alt', 'title')); $this->assertIdentical($conf_form['image_style']['#type'], 'select'); $this->assertIdentical((string) $conf_form['image_style']['#title'], 'Image style'); $this->assertIdentical($conf_form['image_link']['#type'], 'select'); $this->assertIdentical((string) $conf_form['image_link']['#title'], 'Link image to'); $this->assertIdentical($conf_form['alt']['#type'], 'textfield'); $this->assertIdentical((string) $conf_form['alt']['#title'], 'Alternate text'); $this->assertIdentical($conf_form['title']['#type'], 'textfield'); $this->assertIdentical((string) $conf_form['title']['#title'], 'Title'); // Test entity embed using 'Image' Entity Embed Display plugin. $alt_text = "This is sample description"; $title = "This is sample title"; $embed_settings = array('image_link' => 'file'); $content = '<drupal-entity data-entity-type="file" data-entity-uuid="' . $this->image->uuid() . '" data-entity-embed-display="image:image" data-entity-embed-settings=\'' . Json::encode($embed_settings) . '\' alt="' . $alt_text . '" title="' . $title . '">This placeholder should not be rendered.</drupal-entity>'; $settings = array(); $settings['type'] = 'page'; $settings['title'] = 'Test entity embed with image:image'; $settings['body'] = array(array('value' => $content, 'format' => 'custom_format')); $node = $this->drupalCreateNode($settings); $this->drupalGet('node/' . $node->id()); $this->assertRaw($alt_text, 'Alternate text for the embedded image is visible when embed is successful.'); $this->assertNoText(strip_tags($content), 'Placeholder does not appears in the output when embed is successful.'); $this->assertLinkByHref(file_create_url($this->image->getFileUri()), 0, 'Link to the embedded image exists.'); // Embed all three field types in one, to ensure they all render correctly. $content = '<drupal-entity data-entity-type="node" data-entity-uuid="' . $this->node->uuid() . '" data-entity-embed-display="entity_reference:entity_reference_label"></drupal-entity>'; $content .= '<drupal-entity data-entity-type="file" data-entity-uuid="' . $this->file->uuid() . '" data-entity-embed-display="file:file_default"></drupal-entity>'; $content .= '<drupal-entity data-entity-type="file" data-entity-uuid="' . $this->image->uuid() . '" data-entity-embed-display="image:image"></drupal-entity>'; $settings = array(); $settings['type'] = 'page'; $settings['title'] = 'Test node entity embedded first then a file entity'; $settings['body'] = array(array('value' => $content, 'format' => 'custom_format')); $node = $this->drupalCreateNode($settings); $this->drupalGet('node/' . $node->id()); }
/** * {@inheritdoc} */ public function getCropEntity(FileInterface $file, $crop_type) { if (Crop::cropExists($file->getFileUri(), $crop_type)) { /** @var \Drupal\crop\CropInterface $crop */ $crop = Crop::findCrop($file->getFileUri(), $crop_type); } else { $values = [ 'type' => $crop_type, 'entity_id' => $file->id(), 'entity_type' => 'file', 'uri' => $file->getFileUri(), ]; $crop = \Drupal::entityTypeManager() ->getStorage('crop') ->create($values); } return $crop; }
/** * This ensures the resolution of a specific file is within bounds. * The image will be resized if it's too large. */ function testFileValidateImageResolution() { // Non-images. $errors = file_validate_image_resolution($this->nonImage); $this->assertEqual(count($errors), 0, 'Should not get any errors for a non-image file.', 'File'); $errors = file_validate_image_resolution($this->nonImage, '50x50', '100x100'); $this->assertEqual(count($errors), 0, 'Do not check the resolution on non files.', 'File'); // Minimum size. $errors = file_validate_image_resolution($this->image); $this->assertEqual(count($errors), 0, 'No errors for an image when there is no minimum or maximum resolution.', 'File'); $errors = file_validate_image_resolution($this->image, 0, '200x1'); $this->assertEqual(count($errors), 1, 'Got an error for an image that was not wide enough.', 'File'); $errors = file_validate_image_resolution($this->image, 0, '1x200'); $this->assertEqual(count($errors), 1, 'Got an error for an image that was not tall enough.', 'File'); $errors = file_validate_image_resolution($this->image, 0, '200x200'); $this->assertEqual(count($errors), 1, 'Small images report an error.', 'File'); // Maximum size. if ($this->container->get('image.factory')->getToolkitId()) { // Copy the image so that the original doesn't get resized. copy('core/misc/druplicon.png', 'temporary://druplicon.png'); $this->image->setFileUri('temporary://druplicon.png'); $errors = file_validate_image_resolution($this->image, '10x5'); $this->assertEqual(count($errors), 0, 'No errors should be reported when an oversized image can be scaled down.', 'File'); $image = $this->container->get('image.factory')->get($this->image->getFileUri()); $this->assertTrue($image->getWidth() <= 10, 'Image scaled to correct width.', 'File'); $this->assertTrue($image->getHeight() <= 5, 'Image scaled to correct height.', 'File'); // Once again, now with negative width and height to force an error. copy('core/misc/druplicon.png', 'temporary://druplicon.png'); $this->image->setFileUri('temporary://druplicon.png'); $errors = file_validate_image_resolution($this->image, '-10x-5'); $this->assertEqual(count($errors), 1, 'An error reported for an oversized image that can not be scaled down.', 'File'); drupal_unlink('temporary://druplicon.png'); } else { // TODO: should check that the error is returned if no toolkit is available. $errors = file_validate_image_resolution($this->image, '5x10'); $this->assertEqual(count($errors), 1, 'Oversize images that cannot be scaled get an error.', 'File'); } }
/** * Tests that image fields in teasers have correct resources. */ function testNodeTeaser() { // Set the display options for the teaser. $display_options = array('type' => 'image', 'settings' => array('image_style' => 'medium', 'image_link' => 'content')); $display = entity_get_display('node', 'article', 'teaser'); $display->setComponent($this->fieldName, $display_options)->save(); // Render the teaser. $node_render_array = node_view($this->node, 'teaser'); $html = \Drupal::service('renderer')->renderRoot($node_render_array); // Parse the teaser. $parser = new \EasyRdf_Parser_Rdfa(); $graph = new \EasyRdf_Graph(); $base_uri = \Drupal::url('<front>', [], ['absolute' => TRUE]); $parser->parse($graph, $html, 'rdfa', $base_uri); // Construct the node and image URIs for testing. $node_uri = $this->node->url('canonical', ['absolute' => TRUE]); $image_uri = ImageStyle::load('medium')->buildUrl($this->file->getFileUri()); // Test relations from node to image. $expected_value = array('type' => 'uri', 'value' => $image_uri); $this->assertTrue($graph->hasProperty($node_uri, 'http://ogp.me/ns#image', $expected_value), 'Node to file relation found in RDF output (og:image).'); // Test image type. $expected_value = array('type' => 'uri', 'value' => 'http://xmlns.com/foaf/0.1/Image'); $this->assertTrue($graph->hasProperty($image_uri, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', $expected_value), 'Image type found in RDF output (foaf:Image).'); }
/** * Tests for log entry on failing destination. */ function testDrupalMovingUploadedFileError() { // Create a directory and make it not writable. $test_directory = 'test_drupal_move_uploaded_file_fail'; drupal_mkdir('temporary://' . $test_directory, 00); $this->assertTrue(is_dir('temporary://' . $test_directory)); $edit = array('file_subdir' => $test_directory, 'files[file_test_upload]' => drupal_realpath($this->image->getFileUri())); \Drupal::state()->set('file_test.disable_error_collection', TRUE); $this->drupalPostForm('file-test/upload', $edit, t('Submit')); $this->assertResponse(200, 'Received a 200 response for posted test file.'); $this->assertRaw(t('File upload error. Could not move uploaded file.'), 'Found the failure message.'); $this->assertRaw(t('Epic upload FAIL!'), 'Found the failure message.'); // Uploading failed. Now check the log. $this->drupalGet('admin/reports/dblog'); $this->assertResponse(200); $this->assertRaw(t('Upload error. Could not move uploaded file @file to destination @destination.', array('@file' => $this->image->getFilename(), '@destination' => 'temporary://' . $test_directory . '/' . $this->image->getFilename())), 'Found upload error log entry.'); }
/** * Asserts that two files are the same by comparing the fid and filepath. * * @param \Drupal\file\FileInterface $file1 * File object to compare. * @param \Drupal\file\FileInterface $file2 * File object to compare. */ function assertSameFile(FileInterface $file1, FileInterface $file2) { $this->assertEqual($file1->id(), $file2->id(), t('Files have the same ids: %file1 == %file2.', array('%file1' => $file1->id(), '%file2-fid' => $file2->id())), 'Same file'); $this->assertEqual($file1->getFileUri(), $file2->getFileUri(), t('Files have the same path: %file1 == %file2.', array('%file1' => $file1->getFileUri(), '%file2' => $file2->getFileUri())), 'Same file'); }
/** * Asserts that a file's status is set to permanent in the database. */ function assertFileIsPermanent(FileInterface $file, $message = NULL) { $message = isset($message) ? $message : format_string('File %file is permanent.', array('%file' => $file->getFileUri())); $this->assertTrue($file->isPermanent(), $message); }
/** * Utility to style an individual file entity for use in a Juicebox gallery. * * This method can detect if the passed file is incompatible with Juicebox. * If so it styles the output as a mimetype image icon representing the file * type. Otherwise the item is styled normally with the passed image style. * * @param FileInterface $file * A file entity containing the image data to append Juicebox styled image * data to. * @param string $style * The Drupal image style to apply to the item. * @param boolean $check_compatible * Whether-or-not to detect if the item is compatible with Juicebox, and if * so, substitute a mimetype icon for its output. * @return array * The styled image data. */ protected function styleImage(FileInterface $file, $style, $check_compatible = TRUE) { $global_settings = $this->getGlobalSettings(); $library = $this->getLibrary(); $mimetype = $file->getMimeType(); $image_data = array(); $image_data['juicebox_compatible'] = TRUE; // Set the normal, unstyled, url for reference. $image_data['unstyled_src'] = file_create_url($file->getFileUri()); // Check compatibility if configured and if the library info contains // mimetype compatibitly information. if ($check_compatible && !empty($library['compatible_mimetypes']) && !in_array($mimetype, $library['compatible_mimetypes'])) { // If the item is not compatible, find the substitute mimetype icon. $image_data['juicebox_compatible'] = FALSE; $icon_dir = drupal_get_path('module', 'juicebox') . '/images/mimetypes'; // We only have icons for each major type, so simplify accordingly. // file_icon_class() could also be useful here but would require // supporting icons for more package types. $type_parts = explode('/', $mimetype); $icon_path = $icon_dir . '/' . reset($type_parts) . '.png'; if (file_exists($icon_path)) { $image_data['imageURL'] = file_create_url($icon_path); } else { $image_data['imageURL'] = file_create_url($icon_dir . '/general.png'); } } else { $sizes = array('imageURL' => $style); // The "juicebox_multisize" style is special, and actually consists of 3 // individual styles configured globally. if ($style == 'juicebox_multisize') { $sizes = array('smallImageURL' => $global_settings['juicebox_multisize_small'], 'imageURL' => $global_settings['juicebox_multisize_medium'], 'largeImageURL' => $global_settings['juicebox_multisize_large']); } foreach ($sizes as $size => $style_each) { if (!empty($style_each)) { $style_obj = entity_load('image_style', $style_each); if ($style_obj) { $image_data[$size] = $style_obj->buildUrl($file->getFileUri()); } } else { $image_data[$size] = $image_data['unstyled_src']; } } } return $image_data; }