12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970 |
- <?php
- class ImageFieldTestCase extends DrupalWebTestCase {
- protected $admin_user;
- function setUp() {
- parent::setUp('image');
- $this->admin_user = $this->drupalCreateUser(array('access content', 'access administration pages', 'administer site configuration', 'administer content types', 'administer nodes', 'create article content', 'edit any article content', 'delete any article content', 'administer image styles', 'administer fields'));
- $this->drupalLogin($this->admin_user);
- }
-
- function createImageField($name, $type_name, $field_settings = array(), $instance_settings = array(), $widget_settings = array()) {
- $field = array(
- 'field_name' => $name,
- 'type' => 'image',
- 'settings' => array(),
- 'cardinality' => !empty($field_settings['cardinality']) ? $field_settings['cardinality'] : 1,
- );
- $field['settings'] = array_merge($field['settings'], $field_settings);
- field_create_field($field);
- $instance = array(
- 'field_name' => $field['field_name'],
- 'entity_type' => 'node',
- 'label' => $name,
- 'bundle' => $type_name,
- 'required' => !empty($instance_settings['required']),
- 'settings' => array(),
- 'widget' => array(
- 'type' => 'image_image',
- 'settings' => array(),
- ),
- );
- $instance['settings'] = array_merge($instance['settings'], $instance_settings);
- $instance['widget']['settings'] = array_merge($instance['widget']['settings'], $widget_settings);
- return field_create_instance($instance);
- }
-
- function createRandomStyle() {
- $style_name = strtolower($this->randomName(10));
- $style_label = $this->randomString();
- image_style_save(array('name' => $style_name, 'label' => $style_label));
- $style_path = 'admin/config/media/image-styles/edit/' . $style_name;
- return array(
- 'name' => $style_name,
- 'label' => $style_label,
- 'path' => $style_path,
- );
- }
-
- function uploadNodeImage($image, $field_name, $type) {
- $edit = array(
- 'title' => $this->randomName(),
- );
- $edit['files[' . $field_name . '_' . LANGUAGE_NONE . '_0]'] = drupal_realpath($image->uri);
- $this->drupalPost('node/add/' . $type, $edit, t('Save'));
-
- $matches = array();
- preg_match('/node\/([0-9]+)/', $this->getUrl(), $matches);
- return isset($matches[1]) ? $matches[1] : FALSE;
- }
- }
- class ImageStylesPathAndUrlTestCase extends DrupalWebTestCase {
- protected $style_name;
- protected $image_info;
- protected $image_filepath;
- public static function getInfo() {
- return array(
- 'name' => 'Image styles path and URL functions',
- 'description' => 'Tests functions for generating paths and URLs to image styles.',
- 'group' => 'Image',
- );
- }
- function setUp() {
- parent::setUp('image_module_test');
- $this->style_name = 'style_foo';
- image_style_save(array('name' => $this->style_name, 'label' => $this->randomString()));
- }
-
- function testImageStylePath() {
- $scheme = 'public';
- $actual = image_style_path($this->style_name, "$scheme://foo/bar.gif");
- $expected = "$scheme://styles/" . $this->style_name . "/$scheme/foo/bar.gif";
- $this->assertEqual($actual, $expected, 'Got the path for a file URI.');
- $actual = image_style_path($this->style_name, 'foo/bar.gif');
- $expected = "$scheme://styles/" . $this->style_name . "/$scheme/foo/bar.gif";
- $this->assertEqual($actual, $expected, 'Got the path for a relative file path.');
- }
-
- function testImageStyleUrlAndPathPublic() {
- $this->_testImageStyleUrlAndPath('public');
- }
-
- function testImageStyleUrlAndPathPrivate() {
- $this->_testImageStyleUrlAndPath('private');
- }
-
- function testImageStylUrlAndPathPublicUnclean() {
- $this->_testImageStyleUrlAndPath('public', FALSE);
- }
-
- function testImageStyleUrlAndPathPrivateUnclean() {
- $this->_testImageStyleUrlAndPath('private', FALSE);
- }
-
- function testImageStyleUrlExtraSlash() {
- $this->_testImageStyleUrlAndPath('public', TRUE, TRUE);
- }
-
- function testImageStyleUrlForMissingSourceImage() {
- $non_existent_uri = 'public://foo.png';
- $generated_url = image_style_url($this->style_name, $non_existent_uri);
- $this->drupalGet($generated_url);
- $this->assertResponse(404, 'Accessing an image style URL with a source image that does not exist provides a 404 error response.');
- }
-
- function testImageContentTypeHeaders() {
- $files = $this->drupalGetTestFiles('image');
- $file = array_shift($files);
-
- $private_file = file_copy($file, 'private://', FILE_EXISTS_RENAME);
-
- variable_set('image_module_test_invalid_headers', $private_file->uri);
-
- $generated_url = image_style_url($this->style_name, $private_file->uri);
- $this->drupalGet($generated_url);
- variable_del('image_module_test_invalid_headers');
- }
-
- function _testImageStyleUrlAndPath($scheme, $clean_url = TRUE, $extra_slash = FALSE) {
-
-
- variable_set('file_default_scheme', 'temporary');
- variable_set('clean_url', $clean_url);
-
- $directory = $scheme . '://styles/' . $this->style_name;
- $status = file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
- $this->assertNotIdentical(FALSE, $status, 'Created the directory for the generated images for the test style.');
-
- $files = $this->drupalGetTestFiles('image');
- $file = array_shift($files);
- $image_info = image_get_info($file->uri);
- $original_uri = file_unmanaged_copy($file->uri, $scheme . '://', FILE_EXISTS_RENAME);
-
-
- variable_set('image_module_test_file_download', $original_uri);
- $this->assertNotIdentical(FALSE, $original_uri, 'Created the generated image file.');
-
- $generated_uri = image_style_path($this->style_name, $original_uri);
- $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
- $generate_url = image_style_url($this->style_name, $original_uri);
-
-
-
- if ($extra_slash) {
- $modified_uri = str_replace('://', ':///', $original_uri);
- $this->assertNotEqual($original_uri, $modified_uri, 'An extra slash was added to the generated file URI.');
- $generate_url = image_style_url($this->style_name, $modified_uri);
- }
- if (!$clean_url) {
- $this->assertTrue(strpos($generate_url, '?q=') !== FALSE, 'When using non-clean URLS, the system path contains the query string.');
- }
-
- $this->drupalGet(str_replace(IMAGE_DERIVATIVE_TOKEN . '=', IMAGE_DERIVATIVE_TOKEN . '=Zo', $generate_url));
- $this->assertResponse(403, 'Image was inaccessible at the URL with an invalid token.');
-
- $this->drupalGet(str_replace(IMAGE_DERIVATIVE_TOKEN . '=', 'wrongparam=', $generate_url));
- $this->assertResponse(403, 'Image was inaccessible at the URL with a missing token.');
-
-
-
-
- variable_set('file_default_scheme', $scheme);
- $relative_path = file_uri_target($original_uri);
- $generate_url_from_relative_path = image_style_url($this->style_name, $relative_path);
- $this->assertEqual($generate_url, $generate_url_from_relative_path, 'Generated URL is the same regardless of whether it came from a relative path or a file URI.');
- variable_set('file_default_scheme', 'temporary');
-
- $this->drupalGet($generate_url);
- $this->assertResponse(200, 'Image was generated at the URL.');
- $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
- $this->assertRaw(file_get_contents($generated_uri), 'URL returns expected file.');
- $generated_image_info = image_get_info($generated_uri);
- $this->assertEqual($this->drupalGetHeader('Content-Type'), $generated_image_info['mime_type'], 'Expected Content-Type was reported.');
- $this->assertEqual($this->drupalGetHeader('Content-Length'), $generated_image_info['file_size'], 'Expected Content-Length was reported.');
- if ($scheme == 'private') {
- $this->assertEqual($this->drupalGetHeader('Expires'), 'Sun, 19 Nov 1978 05:00:00 GMT', 'Expires header was sent.');
- $this->assertEqual($this->drupalGetHeader('Cache-Control'), 'no-cache, must-revalidate', 'Cache-Control header was set to prevent caching.');
- $this->assertEqual($this->drupalGetHeader('X-Image-Owned-By'), 'image_module_test', 'Expected custom header has been added.');
-
-
- $this->drupalGet($generate_url);
- $this->assertResponse(200, 'Image was generated at the URL.');
-
-
- variable_del('image_module_test_file_download');
- $this->drupalGet($generate_url);
- $this->assertResponse(403, 'Confirmed that access is denied for the private image style.');
-
-
- $file_noaccess = array_shift($files);
- $original_uri_noaccess = file_unmanaged_copy($file_noaccess->uri, $scheme . '://', FILE_EXISTS_RENAME);
- $generated_uri_noaccess = $scheme . '://styles/' . $this->style_name . '/' . $scheme . '/'. drupal_basename($original_uri_noaccess);
- $this->assertFalse(file_exists($generated_uri_noaccess), 'Generated file does not exist.');
- $generate_url_noaccess = image_style_url($this->style_name, $original_uri_noaccess);
- $this->drupalGet($generate_url_noaccess);
- $this->assertResponse(403, 'Confirmed that access is denied for the private image style.');
-
- if (strpos($generate_url, '.png') === FALSE ) {
- $this->fail('Confirming that private image styles are not appended require PNG file.');
- }
- else {
-
-
- $this->assertNoRaw( chr(137) . chr(80) . chr(78) . chr(71) . chr(13) . chr(10) . chr(26) . chr(10), 'No PNG signature found in the response body.');
- }
- }
- elseif ($clean_url) {
-
- $this->drupalGet(str_replace(IMAGE_DERIVATIVE_TOKEN . '=', IMAGE_DERIVATIVE_TOKEN . '=Zo', $generate_url));
- $this->assertResponse(200, 'Existing image was accessible at the URL with an invalid token.');
- }
-
-
- variable_set('image_allow_insecure_derivatives', TRUE);
-
- $files = $this->drupalGetTestFiles('image');
- $file = array_shift($files);
- $image_info = image_get_info($file->uri);
- $original_uri = file_unmanaged_copy($file->uri, $scheme . '://', FILE_EXISTS_RENAME);
-
-
- variable_set('image_module_test_file_download', $original_uri);
-
- $generated_uri = image_style_path($this->style_name, $original_uri);
- $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
- $generate_url = image_style_url($this->style_name, $original_uri);
-
- $this->drupalGet(str_replace(IMAGE_DERIVATIVE_TOKEN . '=', 'wrongparam=', $generate_url));
- $this->assertResponse(200, 'Image was accessible at the URL with a missing token.');
-
-
- $nested_uri = image_style_path($this->style_name, $generated_uri);
- $nested_url = image_style_url($this->style_name, $generated_uri);
- $nested_url_with_wrong_token = str_replace(IMAGE_DERIVATIVE_TOKEN . '=', 'wrongparam=', $nested_url);
- $this->drupalGet($nested_url_with_wrong_token);
- $this->assertResponse(403, 'Image generated from an earlier derivative was inaccessible at the URL with a missing token.');
-
-
- $this->drupalGet(substr_replace($nested_url_with_wrong_token, '//styles/', strrpos($nested_url_with_wrong_token, '/styles/'), strlen('/styles/')));
- $this->assertResponse(403, 'Image generated from an earlier derivative was inaccessible at the URL with a missing token, even with an extra forward slash in the URL.');
- $this->drupalGet(substr_replace($nested_url_with_wrong_token, '/\styles/', strrpos($nested_url_with_wrong_token, '/styles/'), strlen('/styles/')));
- $this->assertResponse(403, 'Image generated from an earlier derivative was inaccessible at the URL with a missing token, even with an extra backslash in the URL.');
-
- $this->drupalGet($nested_url);
- $this->assertResponse(200, 'Image was accessible when a correct token was provided in the URL.');
-
-
-
- variable_set('image_suppress_itok_output', TRUE);
- $generate_url = image_style_url($this->style_name, $original_uri);
- $this->assertIdentical(strpos($generate_url, IMAGE_DERIVATIVE_TOKEN . '='), FALSE, 'The security token does not appear in the image style URL.');
- $this->drupalGet($generate_url);
- $this->assertResponse(200, 'Image was accessible at the URL with a missing token.');
-
-
- $directory = $scheme . '://styles/' . $this->style_name . '/' . $scheme . '/' . $this->randomName();
- $this->drupalGet(file_create_url($directory . '/' . $this->randomName()));
- $this->assertFalse(file_exists($directory), 'New directory was not created in the filesystem when requesting an unauthorized image.');
- }
- }
- class ImageEffectsUnitTest extends ImageToolkitTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Image effects',
- 'description' => 'Test that the image effects pass parameters to the toolkit correctly.',
- 'group' => 'Image',
- );
- }
- function setUp() {
- parent::setUp('image_module_test');
- module_load_include('inc', 'image', 'image.effects');
- }
-
- function testResizeEffect() {
- $this->assertTrue(image_resize_effect($this->image, array('width' => 1, 'height' => 2)), 'Function returned the expected value.');
- $this->assertToolkitOperationsCalled(array('resize'));
-
- $calls = image_test_get_all_calls();
- $this->assertEqual($calls['resize'][0][1], 1, 'Width was passed correctly');
- $this->assertEqual($calls['resize'][0][2], 2, 'Height was passed correctly');
- }
-
- function testScaleEffect() {
-
- $this->assertTrue(image_scale_effect($this->image, array('width' => 10, 'height' => 10)), 'Function returned the expected value.');
- $this->assertToolkitOperationsCalled(array('resize'));
-
- $calls = image_test_get_all_calls();
- $this->assertEqual($calls['resize'][0][1], 10, 'Width was passed correctly');
- $this->assertEqual($calls['resize'][0][2], 5, 'Height was based off aspect ratio and passed correctly');
- }
-
- function testCropEffect() {
-
- $this->assertTrue(image_crop_effect($this->image, array('anchor' => 'top-1', 'width' => 3, 'height' => 4)), 'Function returned the expected value.');
- $this->assertToolkitOperationsCalled(array('crop'));
-
- $calls = image_test_get_all_calls();
- $this->assertEqual($calls['crop'][0][1], 0, 'X was passed correctly');
- $this->assertEqual($calls['crop'][0][2], 1, 'Y was passed correctly');
- $this->assertEqual($calls['crop'][0][3], 3, 'Width was passed correctly');
- $this->assertEqual($calls['crop'][0][4], 4, 'Height was passed correctly');
- }
-
- function testScaleAndCropEffect() {
- $this->assertTrue(image_scale_and_crop_effect($this->image, array('width' => 5, 'height' => 10)), 'Function returned the expected value.');
- $this->assertToolkitOperationsCalled(array('resize', 'crop'));
-
- $calls = image_test_get_all_calls();
- $this->assertEqual($calls['crop'][0][1], 7.5, 'X was computed and passed correctly');
- $this->assertEqual($calls['crop'][0][2], 0, 'Y was computed and passed correctly');
- $this->assertEqual($calls['crop'][0][3], 5, 'Width was computed and passed correctly');
- $this->assertEqual($calls['crop'][0][4], 10, 'Height was computed and passed correctly');
- }
-
- function testDesaturateEffect() {
- $this->assertTrue(image_desaturate_effect($this->image, array()), 'Function returned the expected value.');
- $this->assertToolkitOperationsCalled(array('desaturate'));
-
- $calls = image_test_get_all_calls();
- $this->assertEqual(count($calls['desaturate'][0]), 1, 'Only the image was passed.');
- }
-
- function testRotateEffect() {
-
- $this->assertTrue(image_rotate_effect($this->image, array('degrees' => 90, 'bgcolor' => '#fff')), 'Function returned the expected value.');
- $this->assertToolkitOperationsCalled(array('rotate'));
-
- $calls = image_test_get_all_calls();
- $this->assertEqual($calls['rotate'][0][1], 90, 'Degrees were passed correctly');
- $this->assertEqual($calls['rotate'][0][2], 0xffffff, 'Background color was passed correctly');
- }
-
- function testImageEffectsCaching() {
- $image_effect_definitions_called = &drupal_static('image_module_test_image_effect_info_alter');
-
- $effects = image_effect_definitions();
- $this->assertTrue($image_effect_definitions_called === 1, 'image_effect_definitions() generated data.');
-
- drupal_static_reset('image_effect_definitions');
- drupal_static_reset('image_module_test_image_effect_info_alter');
- $cached_effects = image_effect_definitions();
- $this->assertTrue(is_null($image_effect_definitions_called), 'image_effect_definitions() returned data from cache.');
- $this->assertTrue($effects == $cached_effects, 'Cached effects are the same as generated effects.');
- }
- }
- class ImageAdminUiTestCase extends ImageFieldTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Administrative user interface',
- 'description' => 'Tests the forms used in the administrative user interface.',
- 'group' => 'Image',
- );
- }
- function setUp() {
- parent::setUp(array('image'));
- }
-
- function testAddEffectHelpText() {
-
- $style = $this->createRandomStyle();
-
- $this->drupalGet($style['path'] . '/add/image_crop');
- $this->assertText(t('Cropping will remove portions of an image to make it the specified dimensions.'), 'The image style effect help text was displayed on the add effect page.');
- }
-
- function testEditEffectHelpText() {
-
- $random_style = $this->createRandomStyle();
-
- $edit = array();
- $edit['data[width]'] = 20;
- $edit['data[height]'] = 20;
- $this->drupalPost($random_style['path'] . '/add/image_crop', $edit, t('Add effect'));
-
- drupal_static_reset('image_styles');
- $style = image_style_load($random_style['name']);
- foreach ($style['effects'] as $ieid => $effect) {
- $this->drupalGet($random_style['path'] . '/effects/' . $ieid);
- $this->assertText(t('Cropping will remove portions of an image to make it the specified dimensions.'), 'The image style effect help text was displayed on the edit effect page.');
- }
- }
- }
- class ImageAdminStylesUnitTest extends ImageFieldTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Image styles and effects UI configuration',
- 'description' => 'Tests creation, deletion, and editing of image styles and effects at the UI level.',
- 'group' => 'Image',
- );
- }
-
- function createSampleImage($style) {
- static $file_path;
-
-
- if (!isset($file_path)) {
- $files = $this->drupalGetTestFiles('image');
- $file = reset($files);
- $file_path = file_unmanaged_copy($file->uri);
- }
- return image_style_url($style['name'], $file_path) ? $file_path : FALSE;
- }
-
- function getImageCount($style) {
- return count(file_scan_directory('public://styles/' . $style['name'], '/.*/'));
- }
-
- function testNumericStyleName() {
- $style_name = rand();
- $style_label = $this->randomString();
- $edit = array(
- 'name' => $style_name,
- 'label' => $style_label,
- );
- $this->drupalPost('admin/config/media/image-styles/add', $edit, t('Create new style'));
- $this->assertRaw(t('Style %name was created.', array('%name' => $style_label)), 'Image style successfully created.');
- $options = image_style_options();
- $this->assertTrue(array_key_exists($style_name, $options), format_string('Array key %key exists.', array('%key' => $style_name)));
- }
-
- function testStyle() {
-
- $style_name = strtolower($this->randomName(10));
- $style_label = $this->randomString();
- $style_path = 'admin/config/media/image-styles/edit/' . $style_name;
- $effect_edits = array(
- 'image_resize' => array(
- 'data[width]' => 100,
- 'data[height]' => 101,
- ),
- 'image_scale' => array(
- 'data[width]' => 110,
- 'data[height]' => 111,
- 'data[upscale]' => 1,
- ),
- 'image_scale_and_crop' => array(
- 'data[width]' => 120,
- 'data[height]' => 121,
- ),
- 'image_crop' => array(
- 'data[width]' => 130,
- 'data[height]' => 131,
- 'data[anchor]' => 'center-center',
- ),
- 'image_desaturate' => array(
-
- ),
- 'image_rotate' => array(
- 'data[degrees]' => 5,
- 'data[random]' => 1,
- 'data[bgcolor]' => '#FFFF00',
- ),
- );
-
- $edit = array(
- 'name' => $style_name,
- 'label' => $style_label,
- );
- $this->drupalPost('admin/config/media/image-styles/add', $edit, t('Create new style'));
- $this->assertRaw(t('Style %name was created.', array('%name' => $style_label)), 'Image style successfully created.');
-
-
- foreach ($effect_edits as $effect => $edit) {
-
- $this->drupalPost($style_path, array('new' => $effect), t('Add'));
- if (!empty($edit)) {
- $this->drupalPost(NULL, $edit, t('Add effect'));
- }
- }
-
-
- drupal_static_reset('image_styles');
- $style = image_style_load($style_name);
- foreach ($style['effects'] as $ieid => $effect) {
- $this->drupalGet($style_path . '/effects/' . $ieid);
- foreach ($effect_edits[$effect['name']] as $field => $value) {
- $this->assertFieldByName($field, $value, format_string('The %field field in the %effect effect has the correct value of %value.', array('%field' => $field, '%effect' => $effect['name'], '%value' => $value)));
- }
- }
-
-
-
- $effect_edits_order = array_keys($effect_edits);
- $effects_order = array_values($style['effects']);
- $order_correct = TRUE;
- foreach ($effects_order as $index => $effect) {
- if ($effect_edits_order[$index] != $effect['name']) {
- $order_correct = FALSE;
- }
- }
- $this->assertTrue($order_correct, 'The order of the effects is correctly set by default.');
-
-
- $style_name = strtolower($this->randomName(10));
- $style_label = $this->randomString();
- $weight = count($effect_edits);
- $edit = array(
- 'name' => $style_name,
- 'label' => $style_label,
- );
- foreach ($style['effects'] as $ieid => $effect) {
- $edit['effects[' . $ieid . '][weight]'] = $weight;
- $weight--;
- }
-
- $image_path = $this->createSampleImage($style);
- $this->assertEqual($this->getImageCount($style), 1, format_string('Image style %style image %file successfully generated.', array('%style' => $style['label'], '%file' => $image_path)));
- $this->drupalPost($style_path, $edit, t('Update style'));
-
- $style_path = 'admin/config/media/image-styles/edit/' . $style_name;
-
- $this->drupalGet($style_path);
- $this->assertResponse(200, format_string('Image style %original renamed to %new', array('%original' => $style['label'], '%new' => $style_label)));
-
-
-
- $this->assertEqual($this->getImageCount($style), 0, format_string('Image style %style was flushed after renaming the style and updating the order of effects.', array('%style' => $style['label'])));
-
- drupal_static_reset('image_styles');
- $style = image_style_load($style_name, NULL);
-
- $effect_edits_order = array_reverse($effect_edits_order);
- $effects_order = array_values($style['effects']);
- $order_correct = TRUE;
- foreach ($effects_order as $index => $effect) {
- if ($effect_edits_order[$index] != $effect['name']) {
- $order_correct = FALSE;
- }
- }
- $this->assertTrue($order_correct, 'The order of the effects is correctly set by default.');
-
-
- $image_path = $this->createSampleImage($style);
- $this->assertEqual($this->getImageCount($style), 1, format_string('Image style %style image %file successfully generated.', array('%style' => $style['label'], '%file' => $image_path)));
-
- $effect = array_pop($style['effects']);
- $this->drupalPost($style_path . '/effects/' . $effect['ieid'] . '/delete', array(), t('Delete'));
- $this->assertRaw(t('The image effect %name has been deleted.', array('%name' => $effect['label'])), 'Image effect deleted.');
-
-
- $this->drupalPost('admin/config/media/image-styles/delete/' . $style_name, array(), t('Delete'));
-
- $directory = file_default_scheme() . '://styles/' . $style_name;
- $this->assertFalse(is_dir($directory), format_string('Image style %style directory removed on style deletion.', array('%style' => $style['label'])));
- drupal_static_reset('image_styles');
- $this->assertFalse(image_style_load($style_name), format_string('Image style %style successfully deleted.', array('%style' => $style['label'])));
- }
-
- function testDefaultStyle() {
-
- $style_name = 'thumbnail';
- $style_label = 'Thumbnail (100x100)';
- $edit_path = 'admin/config/media/image-styles/edit/' . $style_name;
- $delete_path = 'admin/config/media/image-styles/delete/' . $style_name;
- $revert_path = 'admin/config/media/image-styles/revert/' . $style_name;
-
- $this->drupalGet($delete_path);
- $this->assertText(t('Page not found'), 'Default styles may not be deleted.');
-
- $this->drupalGet($edit_path);
- $disabled_field = $this->xpath('//input[@id=:id and @disabled="disabled"]', array(':id' => 'edit-name'));
- $this->assertTrue($disabled_field, 'Default styles may not be renamed.');
- $this->assertNoField('edit-submit', 'Default styles may not be edited.');
- $this->assertNoField('edit-add', 'Default styles may not have new effects added.');
-
- drupal_static_reset('image_styles');
- $style = image_style_load($style_name);
- $image_path = $this->createSampleImage($style);
- $this->assertEqual($this->getImageCount($style), 1, format_string('Image style %style image %file successfully generated.', array('%style' => $style['name'], '%file' => $image_path)));
-
- foreach ($style['effects'] as $effect) {
- $this->assertFalse(isset($effect['ieid']), format_string('The %effect effect does not have an ieid.', array('%effect' => $effect['name'])));
- }
-
- $this->drupalPost($edit_path, array(), t('Override defaults'));
- $this->assertRaw(t('The %style style has been overridden, allowing you to change its settings.', array('%style' => $style_label)), 'Default image style may be overridden.');
-
- $this->drupalPost($edit_path, array('new' => 'image_desaturate'), t('Add'));
- drupal_static_reset('image_styles');
- $style = image_style_load($style_name);
-
- foreach ($style['effects'] as $effect) {
- $this->assertTrue(isset($effect['ieid']), format_string('The %effect effect has an ieid.', array('%effect' => $effect['name'])));
- }
-
-
- $effects = array_values($style['effects']);
- $this->assertEqual($effects[0]['name'], 'image_scale', 'The default effect still exists in the overridden style.');
- $this->assertEqual($effects[1]['name'], 'image_desaturate', 'The added effect exists in the overridden style.');
-
- $this->drupalGet($edit_path);
- $disabled_field = $this->xpath('//input[@id=:id and @disabled="disabled"]', array(':id' => 'edit-name'));
- $this->assertFalse($disabled_field, 'Overridden styles may be renamed.');
-
- $image_path = $this->createSampleImage($style);
- $this->assertEqual($this->getImageCount($style), 1, format_string('Image style %style image %file successfully generated.', array('%style' => $style['label'], '%file' => $image_path)));
-
- $this->drupalPost($revert_path, array(), t('Revert'));
- drupal_static_reset('image_styles');
- $style = image_style_load($style_name);
-
- $effects = array_values($style['effects']);
- $this->assertEqual($effects[0]['name'], 'image_scale', 'The default effect still exists in the reverted style.');
- $this->assertFalse(array_key_exists(1, $effects), 'The added effect has been removed in the reverted style.');
- }
-
- function testStyleReplacement() {
-
- $style_name = strtolower($this->randomName(10));
- $style_label = $this->randomString();
- image_style_save(array('name' => $style_name, 'label' => $style_label));
- $style_path = 'admin/config/media/image-styles/edit/' . $style_name;
-
- $field_name = strtolower($this->randomName(10));
- $this->createImageField($field_name, 'article');
- $instance = field_info_instance('node', $field_name, 'article');
- $instance['display']['default']['type'] = 'image';
- $instance['display']['default']['settings']['image_style'] = $style_name;
- field_update_instance($instance);
-
- $test_image = current($this->drupalGetTestFiles('image'));
- $nid = $this->uploadNodeImage($test_image, $field_name, 'article');
- $node = node_load($nid);
-
- $this->drupalGet('node/' . $nid);
- $this->assertRaw(check_plain(image_style_url($style_name, $node->{$field_name}[LANGUAGE_NONE][0]['uri'])), format_string('Image displayed using style @style.', array('@style' => $style_name)));
-
- $new_style_name = strtolower($this->randomName(10));
- $new_style_label = $this->randomString();
- $edit = array(
- 'name' => $new_style_name,
- 'label' => $new_style_label,
- );
- $this->drupalPost('admin/config/media/image-styles/edit/' . $style_name, $edit, t('Update style'));
- $this->assertText(t('Changes to the style have been saved.'), format_string('Style %name was renamed to %new_name.', array('%name' => $style_name, '%new_name' => $new_style_name)));
- $this->drupalGet('node/' . $nid);
- $this->assertRaw(check_plain(image_style_url($new_style_name, $node->{$field_name}[LANGUAGE_NONE][0]['uri'])), format_string('Image displayed using style replacement style.'));
-
- $edit = array(
- 'replacement' => 'thumbnail',
- );
- $this->drupalPost('admin/config/media/image-styles/delete/' . $new_style_name, $edit, t('Delete'));
- $message = t('Style %name was deleted.', array('%name' => $new_style_label));
- $this->assertRaw($message, $message);
- $this->drupalGet('node/' . $nid);
- $this->assertRaw(check_plain(image_style_url('thumbnail', $node->{$field_name}[LANGUAGE_NONE][0]['uri'])), format_string('Image displayed using style replacement style.'));
- }
- }
- class ImageFieldDisplayTestCase extends ImageFieldTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Image field display tests',
- 'description' => 'Test the display of image fields.',
- 'group' => 'Image',
- );
- }
-
- function testImageFieldFormattersPublic() {
- $this->_testImageFieldFormatters('public');
- }
-
- function testImageFieldFormattersPrivate() {
-
- user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array('access content' => FALSE));
- $this->_testImageFieldFormatters('private');
- }
-
- function _testImageFieldFormatters($scheme) {
- $field_name = strtolower($this->randomName());
- $this->createImageField($field_name, 'article', array('uri_scheme' => $scheme));
-
- $test_image = current($this->drupalGetTestFiles('image'));
- $nid = $this->uploadNodeImage($test_image, $field_name, 'article');
- $node = node_load($nid, NULL, TRUE);
-
- $image_uri = $node->{$field_name}[LANGUAGE_NONE][0]['uri'];
- $image_info = array(
- 'path' => $image_uri,
- 'width' => 40,
- 'height' => 20,
- );
- $default_output = theme('image', $image_info);
- $this->assertRaw($default_output, 'Default formatter displaying correctly on full node view.');
-
- $instance = field_info_instance('node', $field_name, 'article');
- $instance['display']['default']['type'] = 'image';
- $instance['display']['default']['settings']['image_link'] = 'file';
- field_update_instance($instance);
- $default_output = l(theme('image', $image_info), file_create_url($image_uri), array('html' => TRUE));
- $this->drupalGet('node/' . $nid);
- $this->assertRaw($default_output, 'Image linked to file formatter displaying correctly on full node view.');
-
- $this->assertEqual(file_get_contents($test_image->uri), $this->drupalGet(file_create_url($image_uri)), 'File was downloaded successfully.');
- if ($scheme == 'private') {
-
-
- $this->assertEqual($this->drupalGetHeader('Content-Type'), 'image/png', 'Content-Type header was sent.');
- $this->assertEqual($this->drupalGetHeader('Cache-Control'), 'private', 'Cache-Control header was sent.');
-
- $this->drupalLogout();
- $this->drupalGet(file_create_url($image_uri));
- $this->assertResponse('403', 'Access denied to original image as anonymous user.');
-
- $this->drupalLogin($this->admin_user);
- }
-
- $instance['display']['default']['settings']['image_link'] = 'content';
- field_update_instance($instance);
- $default_output = l(theme('image', $image_info), 'node/' . $nid, array('html' => TRUE, 'attributes' => array('class' => 'active')));
- $this->drupalGet('node/' . $nid);
- $this->assertRaw($default_output, 'Image linked to content formatter displaying correctly on full node view.');
-
- $instance['display']['default']['settings']['image_link'] = '';
- $instance['display']['default']['settings']['image_style'] = 'thumbnail';
- field_update_instance($instance);
-
-
- $this->drupalGet(image_style_url('thumbnail', $image_uri));
-
-
- $image_info['path'] = image_style_url('thumbnail', $image_uri);
- $image_info['width'] = 100;
- $image_info['height'] = 50;
- $default_output = theme('image', $image_info);
- $this->drupalGet('node/' . $nid);
- $this->assertRaw($default_output, 'Image style thumbnail formatter displaying correctly on full node view.');
- if ($scheme == 'private') {
-
- $this->drupalLogout();
- $this->drupalGet(image_style_url('thumbnail', $image_uri));
- $this->assertResponse('403', 'Access denied to image style thumbnail as anonymous user.');
- }
- }
-
- function testImageFieldSettings() {
- $test_image = current($this->drupalGetTestFiles('image'));
- list(, $test_image_extension) = explode('.', $test_image->filename);
- $field_name = strtolower($this->randomName());
- $instance_settings = array(
- 'alt_field' => 1,
- 'file_extensions' => $test_image_extension,
- 'max_filesize' => '50 KB',
- 'max_resolution' => '100x100',
- 'min_resolution' => '10x10',
- 'title_field' => 1,
- );
- $widget_settings = array(
- 'preview_image_style' => 'medium',
- );
- $field = $this->createImageField($field_name, 'article', array(), $instance_settings, $widget_settings);
- $field['deleted'] = 0;
- $table = _field_sql_storage_tablename($field);
- $schema = drupal_get_schema($table, TRUE);
- $instance = field_info_instance('node', $field_name, 'article');
- $this->drupalGet('node/add/article');
- $this->assertText(t('Files must be less than 50 KB.'), 'Image widget max file size is displayed on article form.');
- $this->assertText(t('Allowed file types: ' . $test_image_extension . '.'), 'Image widget allowed file types displayed on article form.');
- $this->assertText(t('Images must be between 10x10 and 100x100 pixels.'), 'Image widget allowed resolution displayed on article form.');
-
-
- $nid = $this->uploadNodeImage($test_image, $field_name, 'article');
- $this->drupalGet('node/' . $nid . '/edit');
- $this->assertFieldByName($field_name . '[' . LANGUAGE_NONE . '][0][alt]', '', 'Alt field displayed on article form.');
- $this->assertFieldByName($field_name . '[' . LANGUAGE_NONE . '][0][title]', '', 'Title field displayed on article form.');
-
-
- $node = node_load($nid, NULL, TRUE);
- $image_info = array(
- 'path' => image_style_url('medium', $node->{$field_name}[LANGUAGE_NONE][0]['uri']),
- 'width' => 220,
- 'height' => 110,
- );
- $default_output = theme('image', $image_info);
- $this->assertRaw($default_output, "Preview image is displayed using 'medium' style.");
-
- $image_info = array(
- 'path' => $node->{$field_name}[LANGUAGE_NONE][0]['uri'],
- 'alt' => $this->randomName(),
- 'title' => $this->randomName(),
- 'width' => 40,
- 'height' => 20,
- );
- $edit = array(
- $field_name . '[' . LANGUAGE_NONE . '][0][alt]' => $image_info['alt'],
- $field_name . '[' . LANGUAGE_NONE . '][0][title]' => $image_info['title'],
- );
- $this->drupalPost('node/' . $nid . '/edit', $edit, t('Save'));
- $default_output = theme('image', $image_info);
- $this->assertRaw($default_output, 'Image displayed using user supplied alt and title attributes.');
-
- $test_size = 2000;
- $edit = array(
- $field_name . '[' . LANGUAGE_NONE . '][0][alt]' => $this->randomName($test_size),
- $field_name . '[' . LANGUAGE_NONE . '][0][title]' => $this->randomName($test_size),
- );
- $this->drupalPost('node/' . $nid . '/edit', $edit, t('Save'));
- $this->assertRaw(t('Alternate text cannot be longer than %max characters but is currently %length characters long.', array(
- '%max' => $schema['fields'][$field_name .'_alt']['length'],
- '%length' => $test_size,
- )));
- $this->assertRaw(t('Title cannot be longer than %max characters but is currently %length characters long.', array(
- '%max' => $schema['fields'][$field_name .'_title']['length'],
- '%length' => $test_size,
- )));
- }
-
- function testImageFieldFormatterAttributes() {
- $image = theme('image_formatter', array(
- 'item' => array(
- 'uri' => 'http://example.com/example.png',
- 'attributes' => array(
- 'data-image-field-formatter' => 'testFound',
- ),
- 'alt' => t('Image field formatter attribute test.'),
- 'title' => t('Image field formatter'),
- ),
- ));
- $this->assertTrue(stripos($image, 'testFound') > 0, 'Image field formatters can have attributes.');
- }
-
- function testImageFieldDefaultImage() {
-
- $field_name = strtolower($this->randomName());
- $this->createImageField($field_name, 'article');
-
-
- $node = $this->drupalCreateNode(array('type' => 'article'));
- $this->drupalGet('node/' . $node->nid);
-
-
- $this->assertNoPattern('<div class="(.*?)field-name-' . strtr($field_name, '_', '-') . '(.*?)">', 'No image displayed when no image is attached and no default image specified.');
-
- $images = $this->drupalGetTestFiles('image');
- $edit = array(
- 'files[field_settings_default_image]' => drupal_realpath($images[0]->uri),
- );
- $this->drupalPost('admin/structure/types/manage/article/fields/' . $field_name, $edit, t('Save settings'));
-
- field_info_cache_clear();
- $field = field_info_field($field_name);
- $image = file_load($field['settings']['default_image']);
- $this->assertTrue($image->status == FILE_STATUS_PERMANENT, 'The default image status is permanent.');
- $default_output = theme('image', array('path' => $image->uri));
- $this->drupalGet('node/' . $node->nid);
- $this->assertRaw($default_output, 'Default image displayed when no user supplied image is present.');
-
-
- $nid = $this->uploadNodeImage($images[1], $field_name, 'article');
- $node = node_load($nid, NULL, TRUE);
- $image_info = array(
- 'path' => $node->{$field_name}[LANGUAGE_NONE][0]['uri'],
- 'width' => 40,
- 'height' => 20,
- );
- $image_output = theme('image', $image_info);
- $this->drupalGet('node/' . $nid);
- $this->assertNoRaw($default_output, 'Default image is not displayed when user supplied image is present.');
- $this->assertRaw($image_output, 'User supplied image is displayed.');
-
- $edit = array(
- 'field[settings][default_image][fid]' => 0,
- );
- $this->drupalPost('admin/structure/types/manage/article/fields/' . $field_name, $edit, t('Save settings'));
-
- field_info_cache_clear();
- $field = field_info_field($field_name);
- $this->assertFalse($field['settings']['default_image'], 'Default image removed from field.');
-
-
- $private_field_name = strtolower($this->randomName());
- $this->createImageField($private_field_name, 'article', array('uri_scheme' => 'private'));
-
- $edit = array(
- 'files[field_settings_default_image]' => drupal_realpath($images[1]->uri),
- );
- $this->drupalPost('admin/structure/types/manage/article/fields/' . $private_field_name, $edit, t('Save settings'));
- $private_field = field_info_field($private_field_name);
- $image = file_load($private_field['settings']['default_image']);
- $this->assertEqual('private', file_uri_scheme($image->uri), 'Default image uses private:// scheme.');
- $this->assertTrue($image->status == FILE_STATUS_PERMANENT, 'The default image status is permanent.');
-
-
- $node = $this->drupalCreateNode(array('type' => 'article'));
- $default_output = theme('image', array('path' => $image->uri));
- $this->drupalGet('node/' . $node->nid);
- $this->assertRaw($default_output, 'Default private image displayed when no user supplied image is present.');
- }
- }
- class ImageFieldValidateTestCase extends ImageFieldTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Image field validation tests',
- 'description' => 'Tests validation functions such as min/max resolution.',
- 'group' => 'Image',
- );
- }
-
- function testResolution() {
- $field_name = strtolower($this->randomName());
- $min_resolution = 50;
- $max_resolution = 100;
- $instance_settings = array(
- 'max_resolution' => $max_resolution . 'x' . $max_resolution,
- 'min_resolution' => $min_resolution . 'x' . $min_resolution,
- );
- $this->createImageField($field_name, 'article', array(), $instance_settings);
-
-
- $image_that_is_too_big = FALSE;
- $image_that_is_too_small = FALSE;
- foreach ($this->drupalGetTestFiles('image') as $image) {
- $info = image_get_info($image->uri);
- if ($info['width'] > $max_resolution) {
- $image_that_is_too_big = $image;
- }
- if ($info['width'] < $min_resolution) {
- $image_that_is_too_small = $image;
- }
- if ($image_that_is_too_small && $image_that_is_too_big) {
- break;
- }
- }
- $nid = $this->uploadNodeImage($image_that_is_too_small, $field_name, 'article');
- $this->assertText(t('The specified file ' . $image_that_is_too_small->filename . ' could not be uploaded. The image is too small; the minimum dimensions are 50x50 pixels.'), 'Node save failed when minimum image resolution was not met.');
- $nid = $this->uploadNodeImage($image_that_is_too_big, $field_name, 'article');
- $this->assertText(t('The image was resized to fit within the maximum allowed dimensions of 100x100 pixels.'), 'Image exceeding max resolution was properly resized.');
- }
- }
- class ImageDimensionsTestCase extends DrupalWebTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Image dimensions',
- 'description' => 'Tests that images have correct dimensions when styled.',
- 'group' => 'Image',
- );
- }
- function setUp() {
- parent::setUp('image_module_test');
- }
-
- function testImageDimensions() {
-
- $files = $this->drupalGetTestFiles('image');
- $file = reset($files);
- $original_uri = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME);
-
- $style = image_style_save(array('name' => 'test', 'label' => 'Test'));
- $generated_uri = 'public://styles/test/public/'. drupal_basename($original_uri);
- $url = image_style_url('test', $original_uri);
- $variables = array(
- 'style_name' => 'test',
- 'path' => $original_uri,
- 'width' => 40,
- 'height' => 20,
- );
-
- $effect = array(
- 'name' => 'image_scale',
- 'data' => array(
- 'width' => 120,
- 'height' => 90,
- 'upscale' => TRUE,
- ),
- 'isid' => $style['isid'],
- );
- image_effect_save($effect);
- $img_tag = theme_image_style($variables);
- $this->assertEqual($img_tag, '<img typeof="foaf:Image" src="' . check_plain($url) . '" width="120" height="60" alt="" />', 'Expected img tag was found.');
- $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_info = image_get_info($generated_uri);
- $this->assertEqual($image_info['width'], 120, 'Expected width was found.');
- $this->assertEqual($image_info['height'], 60, 'Expected height was found.');
-
- $effect = array(
- 'name' => 'image_rotate',
- 'data' => array(
- 'degrees' => -90,
- 'random' => FALSE,
- ),
- 'isid' => $style['isid'],
- );
- image_effect_save($effect);
- $img_tag = theme_image_style($variables);
- $this->assertEqual($img_tag, '<img typeof="foaf:Image" src="' . check_plain($url) . '" width="60" height="120" alt="" />', 'Expected img tag was found.');
- $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_info = image_get_info($generated_uri);
- $this->assertEqual($image_info['width'], 60, 'Expected width was found.');
- $this->assertEqual($image_info['height'], 120, 'Expected height was found.');
-
- $effect = array(
- 'name' => 'image_scale',
- 'data' => array(
- 'width' => 120,
- 'height' => 90,
- 'upscale' => TRUE,
- ),
- 'isid' => $style['isid'],
- );
- image_effect_save($effect);
- $img_tag = theme_image_style($variables);
- $this->assertEqual($img_tag, '<img typeof="foaf:Image" src="' . check_plain($url) . '" width="45" height="90" alt="" />', 'Expected img tag was found.');
- $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_info = image_get_info($generated_uri);
- $this->assertEqual($image_info['width'], 45, 'Expected width was found.');
- $this->assertEqual($image_info['height'], 90, 'Expected height was found.');
-
- $effect = array(
- 'name' => 'image_scale',
- 'data' => array(
- 'width' => 400,
- 'height' => 200,
- 'upscale' => FALSE,
- ),
- 'isid' => $style['isid'],
- );
- image_effect_save($effect);
- $img_tag = theme_image_style($variables);
- $this->assertEqual($img_tag, '<img typeof="foaf:Image" src="' . check_plain($url) . '" width="45" height="90" alt="" />', 'Expected img tag was found.');
- $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_info = image_get_info($generated_uri);
- $this->assertEqual($image_info['width'], 45, 'Expected width was found.');
- $this->assertEqual($image_info['height'], 90, 'Expected height was found.');
-
- $effect = array(
- 'name' => 'image_desaturate',
- 'data' => array(),
- 'isid' => $style['isid'],
- );
- image_effect_save($effect);
- $img_tag = theme_image_style($variables);
- $this->assertEqual($img_tag, '<img typeof="foaf:Image" src="' . check_plain($url) . '" width="45" height="90" alt="" />', 'Expected img tag was found.');
- $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_info = image_get_info($generated_uri);
- $this->assertEqual($image_info['width'], 45, 'Expected width was found.');
- $this->assertEqual($image_info['height'], 90, 'Expected height was found.');
-
- $effect = array(
- 'name' => 'image_rotate',
- 'data' => array(
- 'degrees' => 180,
- 'random' => TRUE,
- ),
- 'isid' => $style['isid'],
- );
- image_effect_save($effect);
- $img_tag = theme_image_style($variables);
- $this->assertEqual($img_tag, '<img typeof="foaf:Image" src="' . check_plain($url) . '" alt="" />', 'Expected img tag was found.');
- $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 = array(
- 'name' => 'image_crop',
- 'data' => array(
- 'width' => 30,
- 'height' => 30,
- 'anchor' => 'center-center',
- ),
- 'isid' => $style['isid'],
- );
- image_effect_save($effect);
- $img_tag = theme_image_style($variables);
- $this->assertEqual($img_tag, '<img typeof="foaf:Image" src="' . check_plain($url) . '" width="30" height="30" alt="" />', 'Expected img tag was found.');
- $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_info = image_get_info($generated_uri);
- $this->assertEqual($image_info['width'], 30, 'Expected width was found.');
- $this->assertEqual($image_info['height'], 30, 'Expected height was found.');
-
- $effect = array(
- 'name' => 'image_rotate',
- 'data' => array(
- 'degrees' => 57,
- 'random' => FALSE,
- ),
- 'isid' => $style['isid'],
- );
- $effect = image_effect_save($effect);
- $img_tag = theme_image_style($variables);
- $this->assertEqual($img_tag, '<img typeof="foaf:Image" src="' . check_plain($url) . '" alt="" />', 'Expected img tag was found.');
- $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_effect_delete($effect);
-
-
- $effect = array(
- 'name' => 'image_module_test_null',
- 'data' => array(),
- 'isid' => $style['isid'],
- );
- image_effect_save($effect);
- $img_tag = theme_image_style($variables);
- $this->assertEqual($img_tag, '<img typeof="foaf:Image" src="' . check_plain($url) . '" alt="" />', 'Expected img tag was found.');
- }
- }
- class ImageDimensionsScaleTestCase extends DrupalUnitTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'image_dimensions_scale()',
- 'description' => 'Tests all control flow branches in image_dimensions_scale().',
- 'group' => 'Image',
- );
- }
-
- function testImageDimensionsScale() {
-
- $test = array();
-
-
-
- $tests[] = array(
- 'input' => array(
- 'dimensions' => array(
- 'width' => 1000,
- 'height' => 2000,
- ),
- 'width' => 200,
- 'height' => NULL,
- 'upscale' => TRUE,
- ),
- 'output' => array(
- 'dimensions' => array(
- 'width' => 200,
- 'height' => 400,
- ),
- 'return_value' => TRUE,
- ),
- );
-
-
-
- $tests[] = array(
- 'input' => array(
- 'dimensions' => array(
- 'width' => 1000,
- 'height' => 800,
- ),
- 'width' => NULL,
- 'height' => 140,
- 'upscale' => FALSE,
- ),
- 'output' => array(
- 'dimensions' => array(
- 'width' => 175,
- 'height' => 140,
- ),
- 'return_value' => TRUE,
- ),
- );
-
-
-
- $tests[] = array(
- 'input' => array(
- 'dimensions' => array(
- 'width' => 8,
- 'height' => 20,
- ),
- 'width' => 200,
- 'height' => 140,
- 'upscale' => TRUE,
- ),
- 'output' => array(
- 'dimensions' => array(
- 'width' => 56,
- 'height' => 140,
- ),
- 'return_value' => TRUE,
- ),
- );
-
- $tests[] = array(
- 'input' => array(
- 'dimensions' => array(
- 'width' => 2000,
- 'height' => 800,
- ),
- 'width' => 200,
- 'height' => 140,
- 'upscale' => FALSE,
- ),
- 'output' => array(
- 'dimensions' => array(
- 'width' => 200,
- 'height' => 80,
- ),
- 'return_value' => TRUE,
- ),
- );
-
- $tests[] = array(
- 'input' => array(
- 'dimensions' => array(
- 'width' => 100,
- 'height' => 50,
- ),
- 'width' => 200,
- 'height' => 140,
- 'upscale' => FALSE,
- ),
- 'output' => array(
- 'dimensions' => array(
- 'width' => 100,
- 'height' => 50,
- ),
- 'return_value' => FALSE,
- ),
- );
- foreach ($tests as $test) {
-
- $return_value = image_dimensions_scale($test['input']['dimensions'], $test['input']['width'], $test['input']['height'], $test['input']['upscale']);
-
- $this->assertEqual($test['output']['dimensions']['width'], $test['input']['dimensions']['width'], format_string('Computed width (@computed_width) equals expected width (@expected_width)', array('@computed_width' => $test['output']['dimensions']['width'], '@expected_width' => $test['input']['dimensions']['width'])));
-
- $this->assertEqual($test['output']['dimensions']['height'], $test['input']['dimensions']['height'], format_string('Computed height (@computed_height) equals expected height (@expected_height)', array('@computed_height' => $test['output']['dimensions']['height'], '@expected_height' => $test['input']['dimensions']['height'])));
-
- $this->assertEqual($test['output']['return_value'], $return_value, 'Correct return value.');
- }
- }
- }
- class ImageFieldDefaultImagesTestCase extends ImageFieldTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Image field default images tests',
- 'description' => 'Tests setting up default images both to the field and field instance.',
- 'group' => 'Image',
- );
- }
- function setUp() {
- parent::setUp(array('field_ui'));
- }
-
- function testDefaultImages() {
-
- $files = $this->drupalGetTestFiles('image');
- $default_images = array();
- foreach (array('field', 'instance', 'instance2', 'field_new', 'instance_new') as $image_target) {
- $file = array_pop($files);
- $file = file_save($file);
- $default_images[$image_target] = $file;
- }
-
- $field_name = strtolower($this->randomName());
- $field_settings = array(
- 'default_image' => $default_images['field']->fid,
- );
- $instance_settings = array(
- 'default_image' => $default_images['instance']->fid,
- );
- $widget_settings = array(
- 'preview_image_style' => 'medium',
- );
- $this->createImageField($field_name, 'article', $field_settings, $instance_settings, $widget_settings);
- $field = field_info_field($field_name);
- $instance = field_info_instance('node', $field_name, 'article');
-
- $instance2 = array_merge($instance, array(
- 'bundle' => 'page',
- 'settings' => array(
- 'default_image' => $default_images['instance2']->fid,
- ),
- ));
- field_create_instance($instance2);
- $instance2 = field_info_instance('node', $field_name, 'page');
-
- $this->drupalGet("admin/structure/types/manage/article/fields/$field_name");
- $this->assertFieldByXpath(
- '//input[@name="field[settings][default_image][fid]"]',
- $default_images['field']->fid,
- format_string(
- 'Article image field default equals expected file ID of @fid.',
- array('@fid' => $default_images['field']->fid)
- )
- );
- $this->assertFieldByXpath(
- '//input[@name="instance[settings][default_image][fid]"]',
- $default_images['instance']->fid,
- format_string(
- 'Article image field instance default equals expected file ID of @fid.',
- array('@fid' => $default_images['instance']->fid)
- )
- );
-
- $this->drupalGet("admin/structure/types/manage/page/fields/$field_name");
- $this->assertFieldByXpath(
- '//input[@name="field[settings][default_image][fid]"]',
- $default_images['field']->fid,
- format_string(
- 'Page image field default equals expected file ID of @fid.',
- array('@fid' => $default_images['field']->fid)
- )
- );
- $this->assertFieldByXpath(
- '//input[@name="instance[settings][default_image][fid]"]',
- $default_images['instance2']->fid,
- format_string(
- 'Page image field instance default equals expected file ID of @fid.',
- array('@fid' => $default_images['instance2']->fid)
- )
- );
-
- $article = $this->drupalCreateNode(array('type' => 'article'));
- $article_built = node_view($article);
- $this->assertEqual(
- $article_built[$field_name]['#items'][0]['fid'],
- $default_images['instance']->fid,
- format_string(
- 'A new article node without an image has the expected default image file ID of @fid.',
- array('@fid' => $default_images['instance']->fid)
- )
- );
-
- $page = $this->drupalCreateNode(array('type' => 'page'));
- $page_built = node_view($page);
- $this->assertEqual(
- $page_built[$field_name]['#items'][0]['fid'],
- $default_images['instance2']->fid,
- format_string(
- 'A new page node without an image has the expected default image file ID of @fid.',
- array('@fid' => $default_images['instance2']->fid)
- )
- );
-
- $field['settings']['default_image'] = $default_images['field_new']->fid;
- field_update_field($field);
-
- $this->drupalGet("admin/structure/types/manage/article/fields/$field_name");
- $this->assertFieldByXpath(
- '//input[@name="field[settings][default_image][fid]"]',
- $default_images['field_new']->fid,
- format_string(
- 'Updated image field default equals expected file ID of @fid.',
- array('@fid' => $default_images['field_new']->fid)
- )
- );
-
- $article_built = node_view($article = node_load($article->nid, NULL, $reset = TRUE));
- $page_built = node_view($page = node_load($page->nid, NULL, $reset = TRUE));
- $this->assertEqual(
- $article_built[$field_name]['#items'][0]['fid'],
- $default_images['instance']->fid,
- format_string(
- 'An existing article node without an image has the expected default image file ID of @fid.',
- array('@fid' => $default_images['instance']->fid)
- )
- );
- $this->assertEqual(
- $page_built[$field_name]['#items'][0]['fid'],
- $default_images['instance2']->fid,
- format_string(
- 'An existing page node without an image has the expected default image file ID of @fid.',
- array('@fid' => $default_images['instance2']->fid)
- )
- );
-
- $instance['settings']['default_image'] = $default_images['instance_new']->fid;
- field_update_instance($instance);
-
-
- $this->drupalGet("admin/structure/types/manage/article/fields/$field_name");
- $this->assertFieldByXpath(
- '//input[@name="instance[settings][default_image][fid]"]',
- $default_images['instance_new']->fid,
- format_string(
- 'Updated article image field instance default equals expected file ID of @fid.',
- array('@fid' => $default_images['instance_new']->fid)
- )
- );
-
- $article_built = node_view($article = node_load($article->nid, NULL, $reset = TRUE));
- $page_built = node_view($page = node_load($page->nid, NULL, $reset = TRUE));
-
- $this->assertEqual(
- $article_built[$field_name]['#items'][0]['fid'],
- $default_images['instance_new']->fid,
- format_string(
- 'An existing article node without an image has the expected default image file ID of @fid.',
- array('@fid' => $default_images['instance_new']->fid)
- )
- );
-
- $this->assertEqual(
- $page_built[$field_name]['#items'][0]['fid'],
- $default_images['instance2']->fid,
- format_string(
- 'An existing page node without an image has the expected default image file ID of @fid.',
- array('@fid' => $default_images['instance2']->fid)
- )
- );
-
- $instance['settings']['default_image'] = NULL;
- field_update_instance($instance);
-
- $this->drupalGet("admin/structure/types/manage/article/fields/$field_name");
- $this->assertFieldByXpath(
- '//input[@name="instance[settings][default_image][fid]"]',
- '',
- 'Updated article image field instance default has been successfully removed.'
- );
-
- $article_built = node_view($article = node_load($article->nid, NULL, $reset = TRUE));
- $page_built = node_view($page = node_load($page->nid, NULL, $reset = TRUE));
-
- $this->assertEqual(
- $article_built[$field_name]['#items'][0]['fid'],
- $default_images['field_new']->fid,
- format_string(
- 'An existing article node without an image has the expected default image file ID of @fid.',
- array('@fid' => $default_images['field_new']->fid)
- )
- );
-
- $this->assertEqual(
- $page_built[$field_name]['#items'][0]['fid'],
- $default_images['instance2']->fid,
- format_string(
- 'An existing page node without an image has the expected default image file ID of @fid.',
- array('@fid' => $default_images['instance2']->fid)
- )
- );
- }
- }
- class ImageThemeFunctionWebTestCase extends DrupalWebTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Image theme functions',
- 'description' => 'Test that the image theme functions work correctly.',
- 'group' => 'Image',
- );
- }
- function setUp() {
- parent::setUp(array('image'));
- }
-
- function testImageFormatterTheme() {
-
- $files = $this->drupalGetTestFiles('image');
- $file = reset($files);
- $original_uri = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME);
-
- image_style_save(array('name' => 'test', 'label' => 'Test'));
- $url = image_style_url('test', $original_uri);
-
-
- $path = $this->randomName();
- $element = array(
- '#theme' => 'image_formatter',
- '#image_style' => 'test',
- '#item' => array(
- 'uri' => $original_uri,
- ),
- '#path' => array(
- 'path' => $path,
- ),
- );
- $rendered_element = render($element);
- $expected_result = '<a href="' . url($path) . '"><img typeof="foaf:Image" src="' . check_plain($url) . '" alt="" /></a>';
- $this->assertEqual($expected_result, $rendered_element, 'theme_image_formatter() correctly renders without title, alt, or path options.');
-
- $fragment = $this->randomName();
- $element['#path']['path'] = '';
- $element['#path']['options'] = array(
- 'external' => TRUE,
- 'fragment' => $fragment,
- );
- $rendered_element = render($element);
- $expected_result = '<a href="#' . $fragment . '"><img typeof="foaf:Image" src="' . check_plain($url) . '" alt="" /></a>';
- $this->assertEqual($expected_result, $rendered_element, 'theme_image_formatter() correctly renders a link fragment.');
- }
- }
- class ImageStyleFlushTest extends ImageFieldTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Image style flushing',
- 'description' => 'Tests flushing of image styles.',
- 'group' => 'Image',
- );
- }
-
- function createSampleImage($style, $wrapper) {
- static $file;
- if (!isset($file)) {
- $files = $this->drupalGetTestFiles('image');
- $file = reset($files);
- }
-
- $source_uri = file_unmanaged_copy($file->uri, $wrapper . '://');
-
- $derivative_uri = image_style_path($style['name'], $source_uri);
- $derivative = image_style_create_derivative($style, $source_uri, $derivative_uri);
- return $derivative ? $derivative_uri : FALSE;
- }
-
- function getImageCount($style, $wrapper) {
- return count(file_scan_directory($wrapper . '://styles/' . $style['name'], '/.*/'));
- }
-
- function testFlush() {
-
- $style_name = strtolower($this->randomName(10));
- $style_label = $this->randomString();
- $style_path = 'admin/config/media/image-styles/edit/' . $style_name;
- $effect_edits = array(
- 'image_resize' => array(
- 'data[width]' => 100,
- 'data[height]' => 101,
- ),
- 'image_scale' => array(
- 'data[width]' => 110,
- 'data[height]' => 111,
- 'data[upscale]' => 1,
- ),
- );
-
- $edit = array(
- 'name' => $style_name,
- 'label' => $style_label,
- );
- $this->drupalPost('admin/config/media/image-styles/add', $edit, t('Create new style'));
-
- foreach ($effect_edits as $effect => $edit) {
-
- $this->drupalPost($style_path, array('new' => $effect), t('Add'));
- if (!empty($edit)) {
- $this->drupalPost(NULL, $edit, t('Add effect'));
- }
- }
-
- $style = image_style_load($style_name);
-
- $image_path = $this->createSampleImage($style, 'public');
-
-
- $this->assertEqual($this->getImageCount($style, 'public'), 2, format_string('Image style %style image %file successfully generated.', array('%style' => $style['name'], '%file' => $image_path)));
-
- $image_path = $this->createSampleImage($style, 'private');
- $this->assertEqual($this->getImageCount($style, 'private'), 1, format_string('Image style %style image %file successfully generated.', array('%style' => $style['name'], '%file' => $image_path)));
-
-
- $effect = array_pop($style['effects']);
- $this->drupalPost($style_path . '/effects/' . $effect['ieid'] . '/delete', array(), t('Delete'));
- $this->assertResponse(200);
- $this->drupalPost($style_path, array(), t('Update style'));
- $this->assertResponse(200);
-
- $this->assertEqual($this->getImageCount($style, 'public'), 1, format_string('Image style %style flushed correctly for %wrapper wrapper.', array('%style' => $style['name'], '%wrapper' => 'public')));
-
- $this->assertEqual($this->getImageCount($style, 'private'), 0, format_string('Image style %style flushed correctly for %wrapper wrapper.', array('%style' => $style['name'], '%wrapper' => 'private')));
- }
- }
|