1

I am working with phpunit and trying to mock a wordpress page. I have done few steps but I count not able to achive a particulat scenario.

Following is My Parent function:

public static function getMyFavouritesList()
    {
        $userId = get_current_user_id();
        $myFavouites = get_user_meta($userId, 'wpfp_favorites', true);
        $myIncidents = get_user_meta($userId, 'favourite_incidents', true);

        $pages = array();
        $forumPosts = array();
        $userPosts = array();
        $incidents = array();

        $pageId = get_the_ID();

        if (is_array($myFavouites)) {
            foreach ($myFavouites as $favourite) {

                if ($pageId == $favourite) {
                    continue;
                }

                $favouritePost = get_post($favourite);
                if (!empty($favouritePost) && is_object($favouritePost)) {

                    // Get post last updated date from post reply
                    $args = array(
                        'orderby' => 'date',
                        'order' => 'DESC',
                        'posts_per_page' => '1',
                        'post_type' => 'reply',
                        'post_parent' => $favouritePost->ID
                    );//Facing issue while providing data to this block.
                    $lastModified = get_posts($args);
                    if (isset($lastModified{0}->post_modified)) { 
                        $postModified = $lastModified{0}->post_modified;
                    } else {
                        $postModified = $favouritePost->post_modified;
                    }

                    $favouriteDetail = array(
                        'id' => $favouritePost->ID,
                        'title' => $favouritePost->post_title,
                        'url' => get_permalink($favouritePost->ID),
                        'date' => date("d F Y", strtotime($postModified)),
                    );
                    if (in_array($favouritePost->post_type, array('page'))) {

                        $pages[] = $favouriteDetail;
                    } elseif (in_array($favouritePost->post_type, array('forum', 'topic'))) {

                        $forumPosts[] = $favouriteDetail;
                    }
                }
            }
        }
}

Following is the PHPunit test code for the above block:

public function testGetMyFavouritesListForPage($post, $currentPostId, $userMeta, $expected, $userPosts)
    {
        $GLOBALS['post'] = $post;
        $GLOBALS['get_ID'] = $currentPostId;
        $GLOBALS['user_meta'] = $userMeta;
        $GLOBALS['posts'] = $userPosts;

        $result = MyFavourites::getMyFavouritesList();
        $this->assertEquals($expected, $result);
    }


public function dataProviderForMyFavourites()
    {
        $page = new \stdClass();
        $page->ID = 1;
        $page->post_title = 'page title';
        $page->post_modified = '2014-06-30 14:50:04';
        $page->post_type = 'page';
$favouriteIncidents = array( 0 => array('id' => '15', 'name' => 'Incident name'));

$incidentsExpected = '<div class="row-fluid favourites-pages">';
        $incidentsExpected .='<h3 class="underlined">Pages</h3>';
        $incidentsExpected .='<table class="phpnew-favourites-list table table-striped table-hover span12">';
        $incidentsExpected .='<tbody><tr><td class="span6"><strong>Page</strong></td>';
        $incidentsExpected .='<td class="span2"><strong>Last updated</strong></td>';
        $incidentsExpected .='<td class="span2"><strong>Remove</strong></td>';
        $incidentsExpected .='<td class="span2"><strong>Print</strong></td></tr>';
        $incidentsExpected .='<tr><td><a href="http://support-centre.com/?p=1">page title</a></td>';
        $incidentsExpected .='<td>26 August 2014</td>';
        $incidentsExpected .='<td><a href="?wpfpaction=remove&postid=1" class="unfavourite"><img src="/wp-content/themes/phpnew/images/grey-star.png" title="Unfavourite" /></a></td>';
        $incidentsExpected .='<td><a href="http://php-centre.com/?p=1?print=pdf" ';
        $incidentsExpected .='target="_blank"><img src="/wp-content/themes/phpnew/images/phpnew-health-print.png" title="Download PDF" /></a></td></tr></tbody></table>';
        $incidentsExpected .='</div><div class="row-fluid favourites-fourms">';
        $incidentsExpected .='<h3 class="underlined">Forum posts</h3>';
        $incidentsExpected .='<p>You have no favourite forum posts</p></div>';
        $incidentsExpected .='<div class="row-fluid favourites-fourms">';
        $incidentsExpected .='<h3 class="underlined">Your posts</h3>';
        $incidentsExpected .="<p>You haven't created any forum posts yet</p></div>";
        $incidentsExpected .='<div class="row-fluid favourites-incidents"><h3 class="underlined">Incidents</h3>';
        $incidentsExpected .='<table class="phpnew-favourites-list table table-striped table-hover span12"><tbody><tr>';
        $incidentsExpected .='<td class="span6"><strong>Incidents</strong></td><td class="span2">&nbsp;</td>';
        $incidentsExpected .='<td class="span2"><strong>Remove</strong></td><td class="span2">&nbsp;</td></tr>';
        $incidentsExpected .='<tr><td><a href="/incidents/?incidentid=15&incidentname=Incident%20name">Incident name</a></td><td>&nbsp;</td>';
        $incidentsExpected .='<td><a href="?incidentid=15&wpfpactionIncident=remove" class="unfavourite">';
        $incidentsExpected .='<img src="/wp-content/themes/phpnew/images/grey-star.png" title="Unfavourite" /></a></td><td>&nbsp;</td></tr>';
        $incidentsExpected .='</tbody></table></div>';
return array(array($page, 1, $favouriteIncidents,  $incidentsExpected, ''));
}

My Problem is I couldnt able to provide date to this block:

$lastModified = get_posts($args);
                    if (isset($lastModified{0}->post_modified)) { 
                        $postModified = $lastModified{0}->post_modified;
                    } else {
                        $postModified = $favouritePost->post_modified;
                    }

Any help.! Thanks!

1 Answer 1

1

The problem is that the code access the database. You should either provide the dataset, so the code loads the correct posts from the database (using either fixtures or factories), or you should stub the get_posts function (you can do this by putting the test in a namespace and defining get_posts function in that namespace)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.