public function testResizedImageInsertion() { $obj = new HtmlEditorFieldTest_Object(); $editor = new HtmlEditorField('Content'); /* * Following stuff is neccessary to * a) use the proper filename for the image we are referencing * b) not confuse the "existing" filesystem by our test */ $imageFile = $this->objFromFixture('Image', 'example_image'); $imageFile->Filename = FRAMEWORK_DIR . '/' . $imageFile->Filename; $origUpdateFilesystem = Config::inst()->get('File', 'update_filesystem'); Config::inst()->update('File', 'update_filesystem', false); $imageFile->write(); Config::inst()->update('File', 'update_filesystem', $origUpdateFilesystem); /* * End of test bet setting */ $editor->setValue('<img src="assets/HTMLEditorFieldTest_example.jpg" width="10" height="20" />'); $editor->saveInto($obj); $parser = new CSSContentParser($obj->Content); $xml = $parser->getByXpath('//img'); $this->assertEquals('', (string) $xml[0]['alt'], 'Alt tags are added by default.'); $this->assertEquals('', (string) $xml[0]['title'], 'Title tags are added by default.'); $this->assertEquals(10, (int) $xml[0]['width'], 'Width tag of resized image is set.'); $this->assertEquals(20, (int) $xml[0]['height'], 'Height tag of resized image is set.'); $neededFilename = 'assets/_resampled/ResizedImage' . Convert::base64url_encode(array(10, 20)) . '/HTMLEditorFieldTest_example.jpg'; $this->assertEquals($neededFilename, (string) $xml[0]['src'], 'Correct URL of resized image is set.'); $this->assertTrue(file_exists(BASE_PATH . DIRECTORY_SEPARATOR . $neededFilename), 'File for resized image exists'); $this->assertEquals(false, $obj->HasBrokenFile, 'Referenced image file exists.'); }
/** * Tests {@link Convert::base64url_encode()} and {@link Convert::base64url_decode()} */ public function testBase64url() { $data = 'Wëīrð characters ☺ such as ¤Ø¶÷╬'; // This requires this test file to have UTF-8 character encoding $this->assertEquals($data, Convert::base64url_decode(Convert::base64url_encode($data))); $data = 654.423; $this->assertEquals($data, Convert::base64url_decode(Convert::base64url_encode($data))); $data = true; $this->assertEquals($data, Convert::base64url_decode(Convert::base64url_encode($data))); $data = array('simple', 'array', '¤Ø¶÷╬'); $this->assertEquals($data, Convert::base64url_decode(Convert::base64url_encode($data))); $data = array('a' => 'associative', 4 => 'array', '☺' => '¤Ø¶÷╬'); $this->assertEquals($data, Convert::base64url_decode(Convert::base64url_encode($data))); }
/** * Tests that cached images are regenerated properly after a cached file is renamed with new arguments * ToDo: This doesn't seem like something that is worth testing - what is the point of this? */ public function testRegenerateImagesWithRenaming() { $image = $this->objFromFixture('Image', 'imageWithoutTitle'); $image_generated = $image->ScaleWidth(200); $p = $image_generated->getFullPath(); $this->assertTrue(file_exists($p), 'Resized image exists after creation call'); // Encoding of the arguments is duplicated from cacheFilename $oldArgumentString = Convert::base64url_encode(array(200)); $newArgumentString = Convert::base64url_encode(array(300)); $newPath = str_replace($oldArgumentString, $newArgumentString, $p); if (!file_exists(dirname($newPath))) { mkdir(dirname($newPath)); } $newRelative = str_replace($oldArgumentString, $newArgumentString, $image_generated->getFileName()); rename($p, $newPath); $this->assertFalse(file_exists($p), 'Resized image does not exist at old path after renaming'); $this->assertTrue(file_exists($newPath), 'Resized image exists at new path after renaming'); $this->assertEquals(1, $image->regenerateFormattedImages(), 'Cached images were regenerated in the right number'); $image_generated_2 = new Image_Cached($newRelative); $this->assertEquals(300, $image_generated_2->getWidth(), 'Cached image was regenerated with correct width'); }
public function testCacheFilename() { $image = $this->objFromFixture('Image', 'imageWithoutTitle'); $imageFirst = $image->Pad(200, 200, 'CCCCCC'); $imageFilename = $imageFirst->getURL(); // Encoding of the arguments is duplicated from cacheFilename $neededPart = 'Pad' . Convert::base64url_encode(array(200, 200, 'CCCCCC')); $this->assertContains($neededPart, $imageFilename, 'Filename for cached image is correctly generated'); }
/** * Return the filename for the cached image, given its format name and arguments. * @param string $format The format name. * @return string * @throws InvalidArgumentException */ public function cacheFilename($format) { $args = func_get_args(); array_shift($args); $folder = $this->ParentID ? $this->Parent()->Filename : ASSETS_DIR . "/"; $format = $format . Convert::base64url_encode($args); $filename = $format . "-" . $this->Name; $patterns = $this->getFilenamePatterns($this->Name); if (!preg_match($patterns['FullPattern'], $filename)) { throw new InvalidArgumentException('Filename ' . $filename . ' that should be used to cache a resized image is invalid'); } return $folder . "_resampled/" . $filename; }
/** * Return the filename for the cached image, given its format name and arguments. * @param string $format The format name. * @return string * @throws InvalidArgumentException */ public function cacheFilename($format) { $args = func_get_args(); array_shift($args); // Note: $folder holds the *original* file, while the Image we're working with // may be a formatted image in a child directory (this happens when we're chaining formats) $folder = $this->ParentID ? $this->Parent()->Filename : ASSETS_DIR . "/"; $format = $format . Convert::base64url_encode($args); $filename = $format . "/" . $this->Name; $pattern = $this->getFilenamePatterns($this->Name); // Any previous formats need to be derived from this Image's directory, and prepended to the new filename $prepend = array(); if (($pos = stripos($this->Filename, '_resampled')) !== false) { $candidate = substr($this->Filename, $pos + strlen('_resampled')); preg_match_all($pattern['GeneratorPattern'], $candidate, $matches, PREG_SET_ORDER); foreach ($matches as $formatdir) { $prepend[] = $formatdir[0]; } } $filename = implode($prepend) . $filename; if (!preg_match($pattern['FullPattern'], $filename)) { throw new InvalidArgumentException('Filename ' . $filename . ' that should be used to cache a resized image is invalid'); } return $folder . "_resampled/" . $filename; }