WordPress.org

Make WordPress Core

Ticket #26538: wp-list-authors-unit-tests.diff

File wp-list-authors-unit-tests.diff, 9.4 KB (added by MikeHansenMe, 6 years ago)

Unit tests for wp_list_authors

  • phpunit/tests/author/author-template.php

     
     1<?php
     2
     3class Tests_Author_Template extends WP_UnitTestCase {
     4        var $users;
     5                /* Defaults
     6                'orderby'       => 'name',
     7                'order'         => 'ASC',
     8                'number'        => null,
     9                'optioncount'   => false,
     10                'exclude_admin' => true,
     11                'show_fullname' => false,
     12                'hide_empty'    => true,
     13                'echo'          => true,
     14                'feed'          => [empty string],
     15                'feed_image'    => [empty string],
     16                'feed_type'     => [empty string],
     17                'style'         => 'list',
     18                'html'          => true );
     19                */
     20        function setUp() {
     21                parent::setUp();
     22                $users = array();
     23                $this->users[] = $this->factory->user->create( array( 'user_login' => 'zack', 'display_name' => 'zack', 'role' => 'author', 'first_name' => 'zack', 'last_name' => 'moon' ) );
     24                $this->users[] = $this->factory->user->create( array( 'user_login' => 'bob', 'display_name' => 'bob', 'role' => 'author', 'first_name' => 'bob', 'last_name' => 'reno' ) );
     25                $this->users[] = $this->factory->user->create( array( 'user_login' => 'paul', 'display_name' => 'paul', 'role' => 'author', 'first_name' => 'paul', 'last_name' => 'norris' ) );
     26                $count = 0;
     27                foreach ( $this->users as $userid ) {
     28                        $count = $count + 5;
     29                        for ( $i=0; $i < $count; $i++ ) {
     30                                $post_id = $this->factory->post->create( array( 'post_type' => 'post', 'post_author' => $userid ) );
     31                        }
     32                }
     33        }
     34
     35        function test_default() {
     36                $expected['default'] = '<li><a href="http://example.org/?author=' . $this->users[1] . '" title="Posts by bob">bob</a></li><li><a href="http://example.org/?author=' . $this->users[2] . '" title="Posts by paul">paul</a></li><li><a href="http://example.org/?author=' . $this->users[0] . '" title="Posts by zack">zack</a></li>';
     37                $this->AssertEquals( $expected['default'], wp_list_authors( array( 'echo' => false ) ) );
     38        }
     39
     40        function test_orderby() {
     41                $expected['post_count'] = '<li><a href="http://example.org/?author=' . $this->users[0] . '" title="Posts by zack">zack</a></li><li><a href="http://example.org/?author=' . $this->users[1] . '" title="Posts by bob">bob</a></li><li><a href="http://example.org/?author=' . $this->users[2] . '" title="Posts by paul">paul</a></li>';
     42                $this->AssertEquals( $expected['post_count'], wp_list_authors( array( 'echo' => false, 'orderby' => 'post_count' ) ) );
     43        }
     44
     45        function test_order() {
     46                $expected['id'] = '<li><a href="http://example.org/?author=' . $this->users[2] . '" title="Posts by paul">paul</a></li><li><a href="http://example.org/?author=' . $this->users[1] . '" title="Posts by bob">bob</a></li><li><a href="http://example.org/?author=' . $this->users[0] . '" title="Posts by zack">zack</a></li>';
     47                $this->AssertEquals( $expected['id'], wp_list_authors( array( 'echo' => false, 'orderby' => 'id', 'order' => 'DESC' ) ) );
     48        }
     49       
     50        /*possible bug number 2 return only 1*/
     51        function test_number() {
     52                $expected['number'] = '<li><a href="http://example.org/?author=' . $this->users[1] . '" title="Posts by bob">bob</a></li><li><a href="http://example.org/?author=' . $this->users[2] . '" title="Posts by paul">paul</a></li>';
     53                $this->AssertEquals( $expected['number'], wp_list_authors( array( 'echo' => false, 'number' => 2 ) ) );
     54        }
     55
     56        function test_optioncount() {
     57                $expected['optioncount'] = '<li><a href="http://example.org/?author=' . $this->users[1] . '" title="Posts by bob">bob</a> (10)</li><li><a href="http://example.org/?author=' . $this->users[2] . '" title="Posts by paul">paul</a> (15)</li><li><a href="http://example.org/?author=' . $this->users[0] . '" title="Posts by zack">zack</a> (5)</li>';
     58                $this->AssertEquals( $expected['optioncount'], wp_list_authors( array( 'echo' => false, 'optioncount' => 1 ) ) );
     59        }
     60
     61        function test_exclude_admin() {
     62                $this->factory->post->create( array( 'post_type' => 'post', 'post_author' => 1 ) );
     63                $expected['exclude_admin'] = '<li><a href="http://example.org/?author=1" title="Posts by admin">admin</a></li><li><a href="http://example.org/?author=' . $this->users[1] . '" title="Posts by bob">bob</a></li><li><a href="http://example.org/?author=' . $this->users[2] . '" title="Posts by paul">paul</a></li><li><a href="http://example.org/?author=' . $this->users[0] . '" title="Posts by zack">zack</a></li>';
     64                $this->AssertEquals( $expected['exclude_admin'], wp_list_authors( array( 'echo' => false, 'exclude_admin' => 0 ) ) );
     65        }
     66
     67        function test_show_fullname() {
     68                $expected['show_fullname'] = '<li><a href="http://example.org/?author=' . $this->users[1] . '" title="Posts by bob">bob reno</a></li><li><a href="http://example.org/?author=' . $this->users[2] . '" title="Posts by paul">paul norris</a></li><li><a href="http://example.org/?author=' . $this->users[0] . '" title="Posts by zack">zack moon</a></li>';
     69                $this->AssertEquals( $expected['show_fullname'], wp_list_authors( array( 'echo' => false, 'show_fullname' => 1 ) ) );
     70        }
     71
     72        function test_hide_empty() {
     73                $fred_id = $this->factory->user->create( array( 'user_login' => 'fred', 'role' => 'author' ) );
     74                $expected['hide_empty'] = '<li><a href="http://example.org/?author=' . $this->users[1] . '" title="Posts by bob">bob</a></li><li><a href="http://example.org/?author=' . $fred_id . '" title="Posts by fred">fred</a></li><li><a href="http://example.org/?author=' . $this->users[2] . '" title="Posts by paul">paul</a></li><li><a href="http://example.org/?author=' . $this->users[0] . '" title="Posts by zack">zack</a></li>';
     75                $this->AssertEquals( $expected['hide_empty'], wp_list_authors( array( 'echo' => false, 'hide_empty' => 0 ) ) );
     76        }
     77
     78        function test_echo() {
     79                $expected['echo'] = '<li><a href="http://example.org/?author=' . $this->users[1] . '" title="Posts by bob">bob</a></li><li><a href="http://example.org/?author=' . $this->users[2] . '" title="Posts by paul">paul</a></li><li><a href="http://example.org/?author=' . $this->users[0] . '" title="Posts by zack">zack</a></li>';
     80                ob_start();
     81                wp_list_authors( array( 'echo' => true ) );
     82                $actual = ob_get_clean();
     83                $this->AssertEquals( $expected['echo'], $actual );
     84        }
     85       
     86        function test_feed() {
     87                $expected['feed'] = '<li><a href="http://example.org/?author=' . $this->users[1] . '" title="Posts by bob">bob</a> (<a href="http://example.org/?feed=rss2&amp;author=' . $this->users[1] . '" title="link to feed">link to feed</a>)</li><li><a href="http://example.org/?author=' . $this->users[2] . '" title="Posts by paul">paul</a> (<a href="http://example.org/?feed=rss2&amp;author=' . $this->users[2] . '" title="link to feed">link to feed</a>)</li><li><a href="http://example.org/?author=' . $this->users[0] . '" title="Posts by zack">zack</a> (<a href="http://example.org/?feed=rss2&amp;author=' . $this->users[0] . '" title="link to feed">link to feed</a>)</li>';
     88                $this->AssertEquals( $expected['feed'], wp_list_authors( array( 'echo' => false, 'feed' => 'link to feed' ) ) );
     89        }
     90
     91        function test_feed_image() {
     92                $expected['feed_image'] = '<li><a href="http://example.org/?author=' . $this->users[1] . '" title="Posts by bob">bob</a> <a href="http://example.org/?feed=rss2&amp;author=' . $this->users[1] . '"><img src="http://example.com/path/to/a/graphic.png" style="border: none;" /></a></li><li><a href="http://example.org/?author=' . $this->users[2] . '" title="Posts by paul">paul</a> <a href="http://example.org/?feed=rss2&amp;author=' . $this->users[2] . '"><img src="http://example.com/path/to/a/graphic.png" style="border: none;" /></a></li><li><a href="http://example.org/?author=' . $this->users[0] . '" title="Posts by zack">zack</a> <a href="http://example.org/?feed=rss2&amp;author=' . $this->users[0] . '"><img src="http://example.com/path/to/a/graphic.png" style="border: none;" /></a></li>';
     93                $this->AssertEquals( $expected['feed_image'], wp_list_authors( array( 'echo' => false, 'feed_image' => 'example.com/path/to/a/graphic.png' ) ) );
     94        }
     95
     96        /*possible bug: always uses rss2*/
     97        function test_feed_type() {
     98                $expected['feed_type'] = '<li><a href="http://example.org/?author=' . $this->users[1] . '" title="Posts by bob">bob</a> (<a href="http://example.org/?feed=atom&amp;author=' . $this->users[1] . '" title="link to feed">link to feed</a>)</li><li><a href="http://example.org/?author=' . $this->users[2] . '" title="Posts by paul">paul</a> (<a href="http://example.org/?feed=atom&amp;author=' . $this->users[2] . '" title="link to feed">link to feed</a>)</li><li><a href="http://example.org/?author=' . $this->users[0] . '" title="Posts by zack">zack</a> (<a href="http://example.org/?feed=atom&amp;author=' . $this->users[0] . '" title="link to feed">link to feed</a>)</li>';
     99                $this->AssertEquals( $expected['feed_type'], wp_list_authors( array( 'echo' => false, 'feed' => 'link to feed', 'feed_type' => 'atom' ) ) );
     100        }
     101
     102        function test_style() {
     103                $expected['style'] = '<a href="http://example.org/?author=' . $this->users[1] . '" title="Posts by bob">bob</a>, <a href="http://example.org/?author=' . $this->users[2] . '" title="Posts by paul">paul</a>, <a href="http://example.org/?author=' . $this->users[0] . '" title="Posts by zack">zack</a>';
     104                $this->AssertEquals( $expected['style'], wp_list_authors( array( 'echo' => false, 'style' => 'none' ) ) );
     105        }
     106
     107        function test_html() {
     108                $expected['html'] = 'bob, paul, zack';
     109                $this->AssertEquals( $expected['html'], wp_list_authors( array( 'echo' => false, 'html' => 0 ) ) );
     110        }
     111
     112}
     113 No newline at end of file