Skip to content

Commit

Permalink
Merge pull request #814 from helsingborg-stad/test/post-helper-tests
Browse files Browse the repository at this point in the history
test: post helper tests
  • Loading branch information
NiclasNorin authored Mar 21, 2024
2 parents c25758d + db72d84 commit 396729a
Show file tree
Hide file tree
Showing 3 changed files with 621 additions and 25 deletions.
40 changes: 22 additions & 18 deletions library/Helper/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Post
*
* @return object Transformed WP_Post object
*/
public static function preparePostObject($post, $data = null)
public static function preparePostObject(\WP_Post $post, $data = null): object
{
$post = self::complementObject(
$post,
Expand All @@ -43,6 +43,7 @@ public static function preparePostObject($post, $data = null)
],
$data
);

return \Municipio\Helper\FormatObject::camelCase($post);
}

Expand All @@ -52,7 +53,7 @@ public static function preparePostObject($post, $data = null)
* @param object $post WP_Post object
* @param mixed $data Additional data for post object
*/
public static function preparePostObjectSingular($post, $data = null)
public static function preparePostObjectSingular(\WP_Post $post, $data = null): void
{
self::preparePostObject($post, $data);
}
Expand All @@ -66,7 +67,7 @@ public static function preparePostObjectSingular($post, $data = null)
*
* @return object $post Transformed WP_Post object
*/
public static function preparePostObjectArchive($post, $data = null)
public static function preparePostObjectArchive(\WP_Post $post, $data = null): object
{
$post = self::complementObject(
$post,
Expand All @@ -81,25 +82,21 @@ public static function preparePostObjectArchive($post, $data = null)
],
$data
);

return \Municipio\Helper\FormatObject::camelCase($post);
}

/**
* Add post data on post object
*
* @param object $postObject The post object
* @param object $appendFields Data to append on object
* @param array $appendFields Data to append on object
*
* @return object $postObject The post object, with appended data
*/
public static function complementObject($postObject, $appendFields = [], $data = null)
public static function complementObject(\WP_Post $postObject, array $appendFields = [], $data = null): \WP_Post
{
//Check that a post object is entered
if (!is_a($postObject, 'WP_Post')) {
return $postObject;
throw new \WP_Error("Complement object must recive a WP_Post class");
}

$appendFields = apply_filters(
'Municipio/Helper/Post/complementPostObject',
array_merge([], $appendFields) //Ability to add default
Expand Down Expand Up @@ -234,7 +231,7 @@ public static function complementObject($postObject, $appendFields = [], $data =
$postObject->termsUnlinked = self::getPostTerms($postObject->ID, false, $taxonomiesToDisplay);
}

if (!empty($postObject->terms) && in_array('term_icon', $appendFields)) {
if (in_array('term_icon', $appendFields) && !empty($postObject->terms) && !empty($postObject->post_type)) {
$postObject->termIcon = self::getPostTermIcon($postObject->ID, $postObject->post_type);
}

Expand All @@ -245,12 +242,14 @@ public static function complementObject($postObject, $appendFields = [], $data =
$postObject->post_language = $postLang;
}
}

if ($passwordRequired) {
$postObject->post_content = get_the_password_form($postObject);
$postObject->post_content_filtered = get_the_password_form($postObject);
$postObject->post_excerpt = get_the_password_form($postObject);
$postObject->excerpt = get_the_password_form($postObject);
$postObject->excerpt_short = get_the_password_form($postObject);
$postObject->excerpt_shorter = get_the_password_form($postObject);
}

if (in_array('call_to_action_items', $appendFields)) {
Expand All @@ -263,7 +262,7 @@ public static function complementObject($postObject, $appendFields = [], $data =

/* Get location data */
$postObject->location = get_field('location', $postObject->ID);
if (!empty($postObject->location)) {
if (!empty($postObject->location['pin'])) {
$postObject->location['pin'] = \Municipio\Helper\Location::createMapMarker($postObject);
}

Expand Down Expand Up @@ -385,9 +384,11 @@ private static function getPostTermIcon($postId, $postType)
}
}
}

if (empty($termIcon) && !empty($termColor)) {
$termIcon['backgroundColor'] = $color;
}

return \apply_filters('Municipio/Helper/Post/getPostTermIcon', $termIcon);
}

Expand Down Expand Up @@ -472,13 +473,16 @@ private static function removeEmptyPTag($string)
/**
* Get the post featured image
*
* @param integer $postId
* @return array $featuredImage The post thumbnail image, with alt and title
* @param integer $postId Post ID
* @param string|array $size Since as a string (full) or an array [400, 400]
*
* @return array|false $featuredImage The post thumbnail image, with alt and title
*/
public static function getFeaturedImage($postId, $size = 'full')
{
$featuredImageID = get_post_thumbnail_id($postId);
$featuredImage = Image::getImageAttachmentData($featuredImageID, $size);
$thumbnailId = get_post_thumbnail_id($postId);
$featuredImage = !empty($thumbnailId) ? Image::getImageAttachmentData($thumbnailId, $size) : false;


return \apply_filters('Municipio/Helper/Post/FeaturedImage', $featuredImage);
}
Expand All @@ -492,7 +496,7 @@ public static function getFeaturedImage($postId, $size = 'full')
* @param string $posttype The posttype
* @return array Meta keys as array
*/
public static function getPosttypeMetaKeys($postType)
public static function getPosttypeMetaKeys(string $postType)
{
if (!isset(self::$runtimeCache['getPostTypeMetaKeys'])) {
self::$runtimeCache['getPostTypeMetaKeys'] = [];
Expand Down Expand Up @@ -526,7 +530,7 @@ public static function getPosttypeMetaKeys($postType)
/**
* Lists all meta-keys existing for the given post
*
* @param string $post The post id
* @param string $postId The post id
* @return array Meta keys as array
*/
public static function getPostMetaKeys($postId)
Expand Down
8 changes: 1 addition & 7 deletions tests/phpunit/tests/Helper/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@

/**
* Class ImageTest
* @runTestsInSeparateProcesses
*/
class ImageTest extends TestCase
{
/**
* @testdox resize returns false if there is no image ID or Url
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testResizeReturnsFalseIfNoImage()
Expand All @@ -29,7 +29,6 @@ public function testResizeReturnsFalseIfNoImage()

/**
* @testdox resize returns a resized image when a correct image ID is present.
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testResizeReturnsImageIfImageIdIsPresent()
Expand All @@ -46,7 +45,6 @@ public function testResizeReturnsImageIfImageIdIsPresent()

/**
* @testdox resize returns a resized image when a correct image URL is present.
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testResizeReturnsImageIfImageUrlIsPresent()
Expand Down Expand Up @@ -88,7 +86,6 @@ public function testResizeReturnsOriginalImage()

/**
* @testdox urlToPath Null if global server variable isnt available.
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testUrlToPathReturnsNullIfGlobalServerIsNotSet()
Expand All @@ -102,7 +99,6 @@ public function testUrlToPathReturnsNullIfGlobalServerIsNotSet()

/**
* @testdox urlToPath Path url is provided.
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testUrlToPathReturnsPathIfUrlIsProvided()
Expand All @@ -119,7 +115,6 @@ public function testUrlToPathReturnsPathIfUrlIsProvided()

/**
* @testdox pathToUrl Returns Null if global server variable isnt available.
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testPathToUrlReturnsNullIfGlobalServerIsNotSet()
Expand All @@ -133,7 +128,6 @@ public function testPathToUrlReturnsNullIfGlobalServerIsNotSet()

/**
* @testdox pathToUrl Returns URL when path is provided.
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testPathToUrlReturnsUrlWhenPathProvided()
Expand Down
Loading

0 comments on commit 396729a

Please sign in to comment.