/** * Asserts that two files have the same values (except timestamp). * * @param \Drupal\file\FileInterface $before * File object to compare. * @param \Drupal\file\FileInterface $after * File object to compare. */ function assertFileUnchanged(FileInterface $before, FileInterface $after) { $this->assertEqual($before->id(), $after->id(), t('File id is the same: %file1 == %file2.', array('%file1' => $before->id(), '%file2' => $after->id())), 'File unchanged'); $this->assertEqual($before->getOwner()->id(), $after->getOwner()->id(), t('File owner is the same: %file1 == %file2.', array('%file1' => $before->getOwner()->id(), '%file2' => $after->getOwner()->id())), 'File unchanged'); $this->assertEqual($before->getFilename(), $after->getFilename(), t('File name is the same: %file1 == %file2.', array('%file1' => $before->getFilename(), '%file2' => $after->getFilename())), 'File unchanged'); $this->assertEqual($before->getFileUri(), $after->getFileUri(), t('File path is the same: %file1 == %file2.', array('%file1' => $before->getFileUri(), '%file2' => $after->getFileUri())), 'File unchanged'); $this->assertEqual($before->getMimeType(), $after->getMimeType(), t('File MIME type is the same: %file1 == %file2.', array('%file1' => $before->getMimeType(), '%file2' => $after->getMimeType())), 'File unchanged'); $this->assertEqual($before->getSize(), $after->getSize(), t('File size is the same: %file1 == %file2.', array('%file1' => $before->getSize(), '%file2' => $after->getSize())), 'File unchanged'); $this->assertEqual($before->isPermanent(), $after->isPermanent(), t('File status is the same: %file1 == %file2.', array('%file1' => $before->isPermanent(), '%file2' => $after->isPermanent())), 'File unchanged'); }
/** * 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; }