Skip to content

Commit

Permalink
Allowing leading \t for API fields in a photo object. Closes #1273
Browse files Browse the repository at this point in the history
  • Loading branch information
jmathai committed May 21, 2013
1 parent 6902c67 commit bd8b294
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/libraries/models/Photo.php
Original file line number Diff line number Diff line change
Expand Up @@ -623,8 +623,9 @@ public function update($id, $attributes = array())
if(isset($attributes['tags']) && !empty($attributes['tags']))
$attributes['tags'] = $tagObj->sanitizeTagsAsString($attributes['tags']);

// trim all the attributes
foreach($attributes as $key => $val)
$attributes[$key] = trim($val);
$attributes[$key] = $this->trim($val);

// since tags can be created adhoc we need to ensure they're here
if(isset($attributes['tags']) && !empty($attributes['tags']))
Expand Down Expand Up @@ -730,6 +731,11 @@ public function replace($id, $localFile, $name, $attributes = array())
return false;
}
}

// trim all the attributes
foreach($attributes as $key => $val)
$attributes[$key] = $this->trim($val);

// update photo paths / hash
$updPathsResp = $this->db->postPhoto($id, $attributes);

Expand Down Expand Up @@ -908,6 +914,11 @@ public function upload($localFile, $name, $attributes = array())
),
$attributes
);

// trim all the attributes
foreach($attributes as $key => $val)
$attributes[$key] = $this->trim($val);

$stored = $this->db->putPhoto($id, $attributes, $dateTaken);
unlink($localFile);
unlink($resp['localFileCopy']);
Expand Down Expand Up @@ -1042,6 +1053,11 @@ protected function autoRotateEnabled($allowAutoRotate)
return false;
}

protected function trim($string)
{
return preg_replace('/^([ \r\n]+)|(\s+)$/', '', $string);
}

private function createAndStoreBaseAndOriginal($name, $localFile, $dateTaken, $allowAutoRotate)
{
$paths = $this->generatePaths($name, $dateTaken);
Expand Down Expand Up @@ -1071,7 +1087,6 @@ private function createAndStoreBaseAndOriginal($name, $localFile, $dateTaken, $a
return array('status' => $uploaded, 'paths' => $paths, 'localFileCopy' => $localFileCopy);;
}


/**
* Reads IPTC data from a photo.
*
Expand Down
29 changes: 29 additions & 0 deletions src/tests/libraries/models/PhotoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ public function autoRotateEnabled($allowAutoRotate)
{
return parent::autoRotateEnabled($allowAutoRotate);
}

public function trim($str)
{
return parent::trim($str);
}
}

class PhotoTest extends PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -638,4 +643,28 @@ public function testReadExifAutoRotateEnabledSuccess()

$this->assertEquals($size[1], $exif['width']);
}

public function testTrim()
{
// starts
$this->assertEquals("\tabcd", $this->photo->trim("\tabcd"), 'Starts with tab fails');
$this->assertEquals("\t abcd", $this->photo->trim("\t abcd"), 'Starts with tab then space fails');
$this->assertEquals("\tabcd", $this->photo->trim(" \tabcd"), 'Starts with space then tab fails');
$this->assertEquals("abcd", $this->photo->trim("\nabcd"), 'Starts with new line fails');
$this->assertEquals("\tabcd", $this->photo->trim("\n\tabcd"), 'Starts with new line then tab fails');

// ends
$this->assertEquals("abcd", $this->photo->trim("abcd\t"), 'Ends with tab fails');
$this->assertEquals("abcd", $this->photo->trim("abcd\t "), 'Ends with tab then space fails');
$this->assertEquals("abcd", $this->photo->trim("abcd \t"), 'Ends with space then tab fails');
$this->assertEquals("abcd", $this->photo->trim("abcd\n"), 'Ends with new line fails');
$this->assertEquals("abcd", $this->photo->trim("abcd\n\t"), 'Ends with new line then tab fails');

// starts and ends
$this->assertEquals("\tabcd", $this->photo->trim("\tabcd\t"), 'Starts/Ends with tab fails');
$this->assertEquals("\t abcd", $this->photo->trim("\t abcd\t "), 'Starts/Ends with tab then space fails');
$this->assertEquals("\tabcd", $this->photo->trim(" \tabcd \t"), 'Starts/Ends with space then tab fails');
$this->assertEquals("abcd", $this->photo->trim("\nabcd\n"), 'Starts/Ends with new line fails');
$this->assertEquals("\tabcd", $this->photo->trim("\n\tabcd\n\t"), 'Starts/Ends with new line then tab fails');
}
}

0 comments on commit bd8b294

Please sign in to comment.