Skip to content

Commit

Permalink
edited test
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Wagner committed Feb 15, 2018
1 parent f58877c commit 2907fad
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 61 deletions.
22 changes: 13 additions & 9 deletions src/Image/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,16 @@

class Image
{
public function addToTemplateData(string $imageField, string $imageSelectorField, array &$templateData, array $item, int $maxWidth = null, string $lightboxId = null,
string $lightboxName = null, FilesModel $model = null)
{
public function addToTemplateData(
string $imageField,
string $imageSelectorField,
array &$templateData,
array $item,
int $maxWidth = null,
string $lightboxId = null,
string $lightboxName = null,
FilesModel $model = null
) {
$containerUtil = System::getContainer()->get('huh.utils.container');

try {
Expand Down Expand Up @@ -81,8 +88,7 @@ public function addToTemplateData(string $imageField, string $imageSelectorField
}

try {
$src =
System::getContainer()->get('contao.image.image_factory')->create(TL_ROOT.'/'.$item[$imageField], $size)->getUrl(TL_ROOT);
$src = System::getContainer()->get('contao.image.image_factory')->create(TL_ROOT.'/'.$item[$imageField], $size)->getUrl(TL_ROOT);
$picture = System::getContainer()->get('contao.image.picture_factory')->create(TL_ROOT.'/'.$item[$imageField], $size);

$picture = [
Expand All @@ -101,7 +107,7 @@ public function addToTemplateData(string $imageField, string $imageSelectorField
}

// Image dimensions
if ($file->exists() && false !== ($imgSize = $file->imageSize)) {
if (false !== ($imgSize = $file->imageSize) && $file->exists()) {
$templateData['arrSize'] = $imgSize;
$templateData['imgSize'] = ' width="'.$imgSize[0].'" height="'.$imgSize[1].'"';
}
Expand Down Expand Up @@ -197,9 +203,7 @@ public function addToTemplateData(string $imageField, string $imageSelectorField
$templateData['attributes'] = ' target="_blank"';
}
}
}

// Fullsize view
} // Fullsize view
elseif ($item['fullsize'] && $containerUtil->isFrontend()) {
$templateData[$hrefKey] = TL_FILES_URL.System::urlEncode($item[$imageField]);
$templateData['attributes'] = ' data-lightbox="'.substr($lightboxId, 9, -1).'"';
Expand Down
91 changes: 42 additions & 49 deletions src/Url/UrlUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,67 +135,31 @@ public function getJumpToPageObject(int $jumpTo, bool $fallbackToObjPage = true)
}

/**
* Prepare URL from ID and keep query string from current string.
*
* @param string|int|null
*
* @return string
*/
protected function prepareUrl($url)
{
if (null === $url) {
$url = Environment::get('requestUri');
} elseif (is_numeric($url)) {
if (null === ($jumpTo = PageModel::findByPk($url))) {
throw new \InvalidArgumentException('Given page id does not exist.');
}

$url = Controller::generateFrontendUrl($jumpTo->row());

list(, $queryString) = explode('?', Environment::get('request'), 2);

if ('' != $queryString) {
$url .= '?'.$queryString;
}
}

$url = ampersand($url, false);

return $url;
}

/**
* Redirect to another page
* Redirect to another page.
*
* @param string $strLocation The target URL
* @param integer $intStatus The HTTP status code (defaults to 303)
* @param string $strLocation The target URL
* @param int $intStatus The HTTP status code (defaults to 303)
*/
public function redirect($strLocation, $intStatus=303)
public function redirect($strLocation, $intStatus = 303)
{
if (headers_sent())
{
if (headers_sent()) {
exit;
}

$strLocation = str_replace('&', '&', $strLocation);

// Make the location an absolute URL
if (!preg_match('@^https?://@i', $strLocation))
{
$strLocation = \Environment::get('base') . ltrim($strLocation, '/');
if (!preg_match('@^https?://@i', $strLocation)) {
$strLocation = \Environment::get('base').ltrim($strLocation, '/');
}

// Ajax request
if (\Environment::get('isAjaxRequest'))
{
if (\Environment::get('isAjaxRequest')) {
header('HTTP/1.1 204 No Content');
header('X-Ajax-Location: ' . $strLocation);
}
else
{
header('X-Ajax-Location: '.$strLocation);
} else {
// Add the HTTP header
switch ($intStatus)
{
switch ($intStatus) {
case 301:
header('HTTP/1.1 301 Moved Permanently');
break;
Expand All @@ -213,10 +177,39 @@ public function redirect($strLocation, $intStatus=303)
break;
}

header('Location: ' . $strLocation);
header('Location: '.$strLocation);
}

exit;
}
}

/**
* Prepare URL from ID and keep query string from current string.
*
* @param string|int|null
*
* @return string
*/
protected function prepareUrl($url)
{
if (null === $url) {
$url = Environment::get('requestUri');
} elseif (is_numeric($url)) {
if (null === ($jumpTo = PageModel::findByPk($url))) {
throw new \InvalidArgumentException('Given page id does not exist.');
}

$url = Controller::generateFrontendUrl($jumpTo->row());

list(, $queryString) = explode('?', Environment::get('request'), 2);

if ('' != $queryString) {
$url .= '?'.$queryString;
}
}

$url = ampersand($url, false);

return $url;
}
}
25 changes: 22 additions & 3 deletions tests/Image/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,12 @@ public function testAddToTemplateDataWithoutModel()
$imageArray['singleSRC'] = __DIR__.'/../data/screenshot.jpg';
$imageArray['size'] = 'a:3:{i:0;s:0:"2";i:1;s:0:"2";i:2;s:0:"2";}';
$imageArray['alt'] = '';
$imageArray['fullsize'] = false;
$imageArray['fullsize'] = true;
$imageArray['floating'] = false;
$imageArray['imageUrl'] = __DIR__.'/../data/screenshot.jpg';
$imageArray['linkTitle'] = true;
$imageArray['imageTitle'] = 'imageTitle';
$imageArray['linkTitle'] = false;
$imageArray['id'] = 12;

$templateData['href'] = true;
$templateData['singleSRC'] = [];
Expand All @@ -75,6 +77,9 @@ public function testAddToTemplateDataWithoutModel()

$this->assertNotSame(['href' => true, 'singleSRC' => []], $templateData);
$this->assertSame(__DIR__.'/../data/screenshot.jpg', $templateData['singleSRC']);
$this->assertSame('imageTitle', $templateData['linkTitle']);
$this->assertSame(__DIR__.'/../data/screenshot.jpg', $templateData['imageHref']);
$this->assertSame(' data-lightbox="5dc05b"', $templateData['attributes']);
}

public function testAddToTemplateDataWithModel()
Expand All @@ -91,9 +96,9 @@ public function testAddToTemplateDataWithModel()
$imageArray['size'] = 'a:3:{i:0;s:0:"2";i:1;s:0:"2";i:2;s:0:"2";}';
$imageArray['alt'] = '';
$imageArray['fullsize'] = true;
$imageArray['floating'] = false;
$imageArray['imageUrl'] = __DIR__.'/../data/screenshot.jpg';
$imageArray['linkTitle'] = 'linkTitle';
$imageArray['floating'] = 'floating';
$imageArray['overwriteMeta'] = false;
$imageArray['caption'] = [];
$imageArray['id'] = 12;
Expand All @@ -111,6 +116,7 @@ public function testAddToTemplateDataWithModel()
$this->assertSame(__DIR__.'/../data/screenshot.jpg', $templateData['singleSRC']);
$this->assertSame('margin:10px;', $templateData['margin']);
$this->assertSame('Diebstahl', $templateData['imageTitle']);
$this->assertSame(' float_floating', $templateData['floatClass']);
}

public function testAddToTemplateDataError()
Expand Down Expand Up @@ -150,5 +156,18 @@ public function testAddToTemplateDataError()
$image = new Image();
$image->addToTemplateData('singleSRC', 'addImage', $templateData, $imageArray, 400, 12, 'lightBoxName', $model);
$this->assertSame('', $templateData['src']);

$imageArray['singleSRC'] = '';
$templateData = [];
$templateData['href'] = true;
$imageArray['overwriteMeta'] = true;
$imageArray['fullsize'] = true;
$imageArray['imageUrl'] = __DIR__.'/../data/screensho';
$templateData['singleSRC'] = [];

$image->addToTemplateData('singleSRC', 'addImage', $templateData, $imageArray, 400, 12, 'lightBoxName', $model);
$this->assertNull($templateData['width']);
$this->assertNull($templateData['height']);
$this->assertSame(' target="_blank"', $templateData['attributes']);
}
}

0 comments on commit 2907fad

Please sign in to comment.