public function testImageCacheDeletedAfterDimensionsChange() { $html = $this->_root->getComponentById('root_imageabstract1')->render(); // Normal size $file200 = Kwf_Media::getOutput('Kwc_ImageResponsive_MediaOutput_Image_TestComponent', 'root_imageabstract1', 'dh-200-ffa94c'); // Small size $file100 = Kwf_Media::getOutput('Kwc_ImageResponsive_MediaOutput_Image_TestComponent', 'root_imageabstract1', 'dh-100-ffa94c'); $model = Kwf_Model_Abstract::getInstance('Kwc_ImageResponsive_MediaOutput_Image_TestModel'); $row = $model->getRow('root_imageabstract1'); $row->dimension = 'default2'; $row->kwf_upload_id = 2; $row->save(); $this->_process(); // cache for dh-100 and dh-200 must be deleted, dh-300 and dh-400 remain as zombie $html2 = $this->_root->getComponentById('root_imageabstract1')->render(); $this->assertNotEquals($html, $html2); // Dpr2 size $otherfile200 = Kwf_Media::getOutput('Kwc_ImageResponsive_MediaOutput_Image_TestComponent', 'root_imageabstract1', 'dh-200-ffa94c'); // Normal size $otherfile100 = Kwf_Media::getOutput('Kwc_ImageResponsive_MediaOutput_Image_TestComponent', 'root_imageabstract1', 'dh-100-ffa94c'); // SmallSize cache-id and normal-size cache-id could colide $this->assertNotEquals($file100, $otherfile100); // NormalSize cache-id and Dpr2-size cache-id could colide $this->assertNotEquals($file200, $otherfile200); }
public function indexAction() { Zend_Registry::set('db', false); Kwf_Test_SeparateDb::setDbFromCookie(); // setzt es nur wenn es das cookie wirklich gibt //FnF models setzen damit tests nicht in echte tabellen schreiben Kwf_Component_Cache::setInstance(Kwf_Component_Cache::CACHE_BACKEND_FNF); Kwf_Component_Cache_Memory::setInstance(new Kwf_Component_Cache_MemoryBlackHole()); /* if (class_exists('APCIterator')) { $prefix = Kwf_Cache::getUniquePrefix(); apc_delete_file(new APCIterator('user', '#^'.$prefix.'#')); } else { apc_clear_cache('user'); } */ Kwf_Component_Data_Root::setComponentClass($this->_getParam('root')); Zend_Registry::set('testRootComponentClass', $this->_getParam('root')); $root = Kwf_Component_Data_Root::getInstance(); $root->setFilename('kwf/kwctest/' . $this->_getParam('root')); $url = $this->_getParam('url'); $urlParts = explode('/', $url); if (is_array($urlParts) && $urlParts[0] == 'media') { if (sizeof($urlParts) != 7) { throw new Kwf_Exception_NotFound(); } $class = $urlParts[1]; $id = $urlParts[2]; $type = $urlParts[3]; $checksum = $urlParts[4]; // time() wäre der 5er, wird aber nur wegen browsercache benötigt $filename = $urlParts[6]; if ($checksum != Kwf_Media::getChecksum($class, $id, $type, $filename)) { throw new Kwf_Exception_AccessDenied('Access to file not allowed.'); } Kwf_Media_Output::output(Kwf_Media::getOutput($class, $id, $type)); } if ($url == 'kwf/util/kwc/render') { if (isset($_REQUEST['url'])) { $_REQUEST['url'] = str_replace('/' . $root->filename, '', $_REQUEST['url']); } Kwf_Util_Component::dispatchRender(); } $domain = 'http://' . Zend_Registry::get('config')->server->domain; $data = $root->getPageByUrl($domain . Kwf_Setup::getBaseUrl() . '/' . $url, null); if (!$data) { throw new Kwf_Exception_NotFound(); } $root->setCurrentPage($data); $contentSender = Kwc_Abstract::getSetting($data->componentClass, 'contentSender'); $contentSender = new $contentSender($data); $contentSender->sendContent(true); Kwf_Benchmark::shutDown(); Kwf_Benchmark::output(); exit; }
private function _checkTheSizes($html, $smallImageNum, $smallWidth, $smallHeight) { // getMediaOutput aufrufen, damit Cache-Meta geschrieben wird (wegen d0cf3812b20fa19c40617ac5b08ed08a18ff808d) // muss so gemacht werden, weil der request über getimagesize weiter unten // nicht das FnF-Cache Model dieses Request schreiben kann preg_match('# src=".*/media/([^/]+)/([^/]+)/([^/]+)#', $html, $matches); Kwf_Media::getOutput($matches[1], $matches[2], $matches[3]); $this->assertRegExp('#<img.+?src=".+?' . $smallImageNum . '\\.jpg.+width="' . $smallWidth . '".+height="' . $smallHeight . '"#ms', $html); preg_match('# src="(.+?)"#ms', $html, $matches); $smallSrcSize = getimagesize('http://' . Kwf_Registry::get('testDomain') . $matches[1]); $this->assertEquals($smallWidth, $smallSrcSize[0]); $this->assertEquals($smallHeight, $smallSrcSize[1]); }
public function testClearOutputCacheWhenChangingCropOptions() { $this->assertEquals('dh-{width}-dc16b9', $this->_root->getComponentById('root_page')->getComponent()->getBaseType()); $dim = $this->_root->getComponentById('root_page')->getComponent()->getImageDimensions(); Kwf_Media::clearCache('Kwc_Basic_Image_Crop_ImageComponent', 'root_page', Kwf_Media::DONT_HASH_TYPE_PREFIX . $dim['width'] . '-dc16b9'); Kwc_Basic_Image_Crop_ImageComponent::$getMediaOutputCalled = 0; Kwf_Media::getOutput('Kwc_Basic_Image_Crop_ImageComponent', 'root_page', Kwf_Media::DONT_HASH_TYPE_PREFIX . $dim['width'] . '-dc16b9'); $this->assertEquals(1, Kwc_Basic_Image_Crop_ImageComponent::$getMediaOutputCalled); Kwf_Media::getOutput('Kwc_Basic_Image_Crop_ImageComponent', 'root_page', Kwf_Media::DONT_HASH_TYPE_PREFIX . $dim['width'] . '-dc16b9'); $this->assertEquals(1, Kwc_Basic_Image_Crop_ImageComponent::$getMediaOutputCalled); $c = $this->_root->getComponentById('root_page'); $row = Kwf_Model_Abstract::getInstance('Kwc_Basic_Image_Crop_TestModel')->getRow('root_page'); $row->crop_x = 20; $row->save(); Kwf_Events_ModelObserver::getInstance()->process(); Kwf_Media::getOutput('Kwc_Basic_Image_Crop_ImageComponent', 'root_page', Kwf_Media::DONT_HASH_TYPE_PREFIX . $dim['width'] . '-dc16b9'); $this->assertEquals(2, Kwc_Basic_Image_Crop_ImageComponent::$getMediaOutputCalled); }
public function testOutputCacheWithMtimeFiles() { Kwf_Media_TestMediaOutputClass::$called = 0; $f = tempnam('/tmp', 'outputTest'); Kwf_Media_TestMediaOutputClass::$mtimeFiles = array($f); $id = time() + rand(0, 10000); $o = Kwf_Media::getOutput('Kwf_Media_TestMediaOutputClass', $id, 'mtimeFiles'); unset($o['mtime']); $this->assertEquals(array('mimeType' => 'text/plain', 'contents' => 'foobar' . $id, 'mtimeFiles' => array($f)), $o); $this->assertEquals(1, Kwf_Media_TestMediaOutputClass::$called); $o = Kwf_Media::getOutput('Kwf_Media_TestMediaOutputClass', $id, 'mtimeFiles'); $this->assertEquals(1, Kwf_Media_TestMediaOutputClass::$called); $newTime = time() + 10; $this->assertTrue(touch($f, $newTime)); clearstatcache(); $this->assertEquals($newTime, filemtime($f)); $o = Kwf_Media::getOutput('Kwf_Media_TestMediaOutputClass', $id, 'mtimeFiles'); $this->assertEquals(array('mimeType' => 'text/plain', 'contents' => 'foobar' . $id, 'mtimeFiles' => array($f), 'mtime' => $newTime), $o); $this->assertEquals(2, Kwf_Media_TestMediaOutputClass::$called); $o = Kwf_Media::getOutput('Kwf_Media_TestMediaOutputClass', $id, 'mtimeFiles'); $this->assertEquals(2, Kwf_Media_TestMediaOutputClass::$called); }
public function testParentImage() { $c = $this->_root->getComponentById('root_image-parentImage'); $html = $c->render(true, false); $this->assertTrue(!!preg_match('#<img src="([^"]+)" width="(\\d+)" height="(\\d+)"#', $html, $m)); $this->assertEquals($m[2], 20); $this->assertEquals($m[3], 20); $url = $m[1]; $this->assertTrue(!!preg_match('#/media/([^/]+)/([^/]+)/([^/]+)#', $url, $m)); $this->assertEquals($m[1], 'Kwc_Basic_Image_CacheParentImage_ParentImage_Component'); $this->assertEquals($m[2], 'root_image-parentImage'); $this->assertContains('dh-16-', $m[3]); $o = Kwf_Media::getOutput($m[1], $m[2], $m[3]); $im = new Imagick($o['file']); $this->assertEquals(16, $im->getImageWidth()); $this->assertEquals(16, $im->getImageHeight()); $model = Kwf_Model_Abstract::getInstance('Kwc_Basic_Image_CacheParentImage_Image_TestModel'); $row = $model->getRow('root_image'); $row->kwf_upload_id = 2; $row->save(); $this->_process(); $c = $this->_root->getComponentById('root_image-parentImage'); $html = $c->render(true, false); $this->assertTrue(!!preg_match('#<img src="([^"]+)" width="(\\d+)" height="(\\d+)"#', $html, $m)); $this->assertEquals($m[2], 20); $this->assertEquals($m[3], 7); $url = $m[1]; $this->assertTrue(!!preg_match('#/media/([^/]+)/([^/]+)/([^/]+)#', $url, $m)); $this->assertEquals($m[1], 'Kwc_Basic_Image_CacheParentImage_ParentImage_Component'); $this->assertEquals($m[2], 'root_image-parentImage'); $this->assertContains('dh-20-', $m[3]); $o = Kwf_Media::getOutput($m[1], $m[2], $m[3]); $im = new Imagick(); $im->readImageBlob($o['contents']); $this->assertEquals(20, $im->getImageWidth()); $this->assertEquals(7, $im->getImageHeight()); }
public function testClearCache() { $c = $this->_root->getComponentById('root_page1'); $html = $c->render(); $this->assertTrue(!!preg_match('# src="([^"]+)"#', $html, $m)); $url = str_replace('{width}', '', $m[1]); $url = explode('/', trim($url, '/')); $class = $url[4]; $id = $url[5]; $type = $url[6]; $o = Kwf_Media::getOutput($class, $id, $type); $this->assertNotNull($o); $this->assertEquals('image/jpg', $o['mimeType']); $im = new Imagick(); $im->readImageBlob($o['contents']); $this->assertEquals(26, $im->getImageWidth()); $this->assertEquals(32, $im->getImageHeight()); $row = Kwf_Model_Abstract::getInstance('Kwc_Basic_Image_DprImage_TestModel')->getRow('root_page1'); $row->kwf_upload_id = 3; $row->save(); $this->_process(); $c = $this->_root->getComponentById('root_page1'); $html = $c->render(); $this->assertTrue(!!preg_match('# src="([^"]+)"#', $html, $m)); $url = explode('/', trim($m[1], '/')); $class = $url[4]; $id = $url[5]; $type = $url[6]; $o = Kwf_Media::getOutput($class, $id, $type); $this->assertNotNull($o); $this->assertEquals('image/gif', $o['mimeType']); $im = new Imagick(); $im->readImageBlob($o['contents']); $this->assertEquals(32, $im->getImageWidth()); $this->assertEquals(11, $im->getImageHeight()); }
public static function dispatchMedia() { $requestPath = self::getRequestPath(); if ($requestPath === false) { return; } $baseUrl = Kwf_Setup::getBaseUrl(); if ($baseUrl) { if (substr($requestPath, 0, strlen($baseUrl)) != $baseUrl) { throw new Kwf_Exception_NotFound(); } $requestPath = substr($requestPath, strlen($baseUrl)); } $urlParts = explode('/', substr($requestPath, 1)); if (is_array($urlParts) && $urlParts[0] == 'media') { if (sizeof($urlParts) != 7) { throw new Kwf_Exception_NotFound(); } $class = $urlParts[1]; $id = $urlParts[2]; $type = $urlParts[3]; $checksum = urlencode($urlParts[4]); // time() wäre der 5er, wird aber nur wegen browsercache benötigt $filename = $urlParts[6]; if ($checksum != Kwf_Media::getChecksum($class, $id, $type, $filename)) { throw new Kwf_Exception_NotFound(); } $class = rawurldecode($class); Kwf_Media_Output::output(Kwf_Media::getOutput($class, $id, $type)); } }
public function testEnlargeCacheDeletedOnBaseImageChanged() { // Image and Enlarge have to define different dimensions because else it // could happen that the parent has the same types as the child. // Get EnlargeComponent $component = $this->_root->getChildComponent('1804')->getChildComponent('-linkTag')->getChildComponent('_imagePage'); // Render EnlargeComponent. Request Enlarge-Image (image has to be big enough) preg_match_all('#/media/([^/]+)/([^/]+)/([^/]+)#', $component->render(), $matches); foreach ($matches[0] as $key => $m) { if (strpos($matches[3][$key], '{width}') !== false) { continue; } $fileWithGreaterHeight = Kwf_Media::getOutput($matches[1][$key], $matches[2][$key], $matches[3][$key]); } // Change basis-bild $row = $this->_root->getChildComponent('1804')->getComponent()->getRow(); $row->kwf_upload_id = 5; $row->save(); $this->_process(); // Assert if image cache was changed preg_match_all('#/media/([^/]+)/([^/]+)/([^/]+)#', $component->render(), $matches); foreach ($matches[0] as $key => $m) { if (strpos($matches[3][$key], '{width}') !== false) { continue; } $fileWithSmallerHeight = Kwf_Media::getOutput($matches[1][$key], $matches[2][$key], $matches[3][$key]); } $image1 = new Imagick($fileWithGreaterHeight['file']); $image2 = new Imagick($fileWithSmallerHeight['file']); $this->assertNotEquals($image1->getImageHeight(), $image2->getImageHeight()); }
private function _checkTheSizes($html, $largeImageNum, $largeWidth, $largeHeight, $smallImageNum, $smallWidth, $smallHeight) { // getMediaOutput aufrufen, damit Cache-Meta geschrieben wird (wegen d0cf3812b20fa19c40617ac5b08ed08a18ff808d) // muss so gemacht werden, weil der request über getimagesize weiter unten // nicht das FnF-Cache Model dieses Request schreiben kann preg_match_all('#/media/([^/]+)/([^/]+)/([^/]+)#', $html, $matches); foreach ($matches[0] as $key => $m) { if (substr($matches[3][$key], 0, 10) == 'dh-{width}') { continue; } Kwf_Media::getOutput($matches[1][$key], $matches[2][$key], $matches[3][$key]); } preg_match('#^.*?<a.+?"width":(\\d+),"height":(\\d+).+?<img.+?src=".+?(\\d+)\\.jpg.+width="(\\d+)".+height="(\\d+)".+$#ms', $html, $matches); $this->assertEquals($matches[1], $largeWidth); $this->assertEquals($matches[2], $largeHeight); $this->assertEquals($matches[3], $smallImageNum); $this->assertEquals($matches[4], $smallWidth); $this->assertEquals($matches[5], $smallHeight); preg_match('#href="(.+?)".*? src="(.+?)"#ms', $html, $matches); $smallSrcSize = getimagesize('http://' . Kwf_Registry::get('testDomain') . $matches[2]); $this->assertEquals($smallWidth, $smallSrcSize[0]); $this->assertEquals($smallHeight, $smallSrcSize[1]); preg_match('#data-kwc-lightbox="([^"]*)"#ms', $html, $matches); $lightboxData = json_decode(htmlspecialchars_decode($matches[1]), true); $c = Kwf_Component_Data_Root::getInstance()->getPageByUrl('http://' . Kwf_Registry::get('testDomain') . str_replace('/kwf/kwctest/' . Kwf_Component_Data_Root::getComponentClass(), '', $lightboxData['lightboxUrl']), ''); $c->render(true, true); //first render in-process so we find the cache entry when doing a clear-cache //then render "real" thru http $largeHtml = file_get_contents('http://' . Kwf_Registry::get('testDomain') . $lightboxData['lightboxUrl']); preg_match('#class="lightboxBody.*?<img .*?src="(.*?)"#s', $largeHtml, $matches); $this->assertRegExp('#' . $largeImageNum . '\\.jpg#', $matches[1]); $largeSrcSize = getimagesize('http://' . Kwf_Registry::get('testDomain') . $matches[1]); $this->assertEquals($largeWidth, $largeSrcSize[0]); $this->assertEquals($largeHeight, $largeSrcSize[1]); }
public function testClearOutputCache() { $c = $this->_root->getComponentById('1600'); $this->assertEquals(Kwf_Media::DONT_HASH_TYPE_PREFIX . '{width}-76b7ff', $c->getComponent()->getBaseType()); Kwf_Media::clearCache('Kwc_Basic_Image_FixDimensionComponent', '1600', Kwf_Media::DONT_HASH_TYPE_PREFIX . '16-76b7ff'); Kwc_Basic_Image_FixDimensionComponent::$getMediaOutputCalled = 0; Kwf_Media::getOutput('Kwc_Basic_Image_FixDimensionComponent', '1600', Kwf_Media::DONT_HASH_TYPE_PREFIX . '16-76b7ff'); $this->assertEquals(1, Kwc_Basic_Image_FixDimensionComponent::$getMediaOutputCalled); Kwf_Media::getOutput('Kwc_Basic_Image_FixDimensionComponent', '1600', Kwf_Media::DONT_HASH_TYPE_PREFIX . '16-76b7ff'); $this->assertEquals(1, Kwc_Basic_Image_FixDimensionComponent::$getMediaOutputCalled); Kwf_Media::clearCache('Kwc_Basic_Image_FixDimensionComponent', '1600', Kwf_Media::DONT_HASH_TYPE_PREFIX . '16-76b7ff'); Kwf_Media::getOutput('Kwc_Basic_Image_FixDimensionComponent', '1600', Kwf_Media::DONT_HASH_TYPE_PREFIX . '16-76b7ff'); $this->assertEquals(2, Kwc_Basic_Image_FixDimensionComponent::$getMediaOutputCalled); $row = Kwf_Model_Abstract::getInstance('Kwc_Basic_Image_TestModel')->getRow('1600'); $row->kwf_upload_id = 2; $row->save(); Kwf_Events_ModelObserver::getInstance()->process(); Kwf_Component_Data_Root::reset(); Kwf_Media::getOutput('Kwc_Basic_Image_FixDimensionComponent', '1600', Kwf_Media::DONT_HASH_TYPE_PREFIX . '100-76b7ff'); $this->assertEquals(3, Kwc_Basic_Image_FixDimensionComponent::$getMediaOutputCalled); }