| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | if ( is_multisite() ) : |
|---|
| 4 | |
|---|
| 5 | /** |
|---|
| 6 | * Test site status views in multisite. |
|---|
| 7 | * |
|---|
| 8 | * @group admin |
|---|
| 9 | * @group network-admin |
|---|
| 10 | * @ticket 38071 |
|---|
| 11 | * |
|---|
| 12 | * @todo integrate into wpMSSitesListTable.php after 38071 is accepted |
|---|
| 13 | */ |
|---|
| 14 | class Tests_WP_MS_Sites_List_Table_38071 extends WP_UnitTestCase { |
|---|
| 15 | protected static $site_ids; |
|---|
| 16 | protected $public; |
|---|
| 17 | protected $archived; |
|---|
| 18 | protected $spam; |
|---|
| 19 | protected $mature; |
|---|
| 20 | protected $deleted; |
|---|
| 21 | |
|---|
| 22 | /** |
|---|
| 23 | * @var WP_MS_Sites_List_Table |
|---|
| 24 | */ |
|---|
| 25 | var $table = false; |
|---|
| 26 | |
|---|
| 27 | function setUp() { |
|---|
| 28 | parent::setUp(); |
|---|
| 29 | $this->table = _get_list_table( 'WP_MS_Sites_List_Table', array( 'screen' => 'ms-sites' ) ); |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | public static function wpSetUpBeforeClass( $factory ) { |
|---|
| 33 | self::$site_ids = array( |
|---|
| 34 | 'wordpress.org/' => array( 'domain' => 'wordpress.org', 'path' => '/', |
|---|
| 35 | 'meta' => array( 'public' => 1, 'archived' => 0, 'mature' => 0, 'spam' => 0, 'deleted' => 0 ) ), |
|---|
| 36 | 'wordpress.org/foo/' => array( 'domain' => 'wordpress.org', 'path' => '/foo/', |
|---|
| 37 | 'meta' => array( 'public' => 1, 'archived' => 1, 'mature' => 0, 'spam' => 0, 'deleted' => 0 ) ), |
|---|
| 38 | 'wordpress.org/foo/bar/' => array( 'domain' => 'wordpress.org', 'path' => '/foo/bar/', |
|---|
| 39 | 'meta' => array( 'public' => 0, 'archived' => 1, 'mature' => 1, 'spam' => 0, 'deleted' => 0 ) ), |
|---|
| 40 | 'wordpress.org/afoo/' => array( 'domain' => 'wordpress.org', 'path' => '/afoo/', |
|---|
| 41 | 'meta' => array( 'public' => 1, 'archived' => 0, 'mature' => 1, 'spam' => 0, 'deleted' => 0 ) ), |
|---|
| 42 | 'make.wordpress.org/' => array( 'domain' => 'make.wordpress.org', 'path' => '/', |
|---|
| 43 | 'meta' => array( 'public' => 0, 'archived' => 0, 'mature' => 0, 'spam' => 1, 'deleted' => 0 ) ), |
|---|
| 44 | 'make.wordpress.org/foo/' => array( 'domain' => 'make.wordpress.org', 'path' => '/foo/', |
|---|
| 45 | 'meta' => array( 'public' => 0, 'archived' => 0, 'mature' => 0, 'spam' => 1, 'deleted' => 1 ) ), |
|---|
| 46 | 'www.w.org/' => array( 'domain' => 'www.w.org', 'path' => '/', |
|---|
| 47 | 'meta' => array( 'public' => 1, 'archived' => 0, 'mature' => 0, 'spam' => 0, 'deleted' => 0 ) ), |
|---|
| 48 | 'www.w.org/foo/' => array( 'domain' => 'www.w.org', 'path' => '/foo/', |
|---|
| 49 | 'meta' => array( 'public' => 1, 'archived' => 0, 'mature' => 0, 'spam' => 0, 'deleted' => 1 ) ), |
|---|
| 50 | 'www.w.org/foo/bar/' => array( 'domain' => 'www.w.org', 'path' => '/foo/bar/', |
|---|
| 51 | 'meta' => array( 'public' => 1, 'archived' => 1, 'mature' => 1, 'spam' => 0, 'deleted' => 0 ) ), |
|---|
| 52 | 'test.example.org/' => array( 'domain' => 'test.example.org', 'path' => '/', |
|---|
| 53 | 'meta' => array( 'public' => 0, 'archived' => 0, 'mature' => 0, 'spam' => 0, 'deleted' => 1 ) ), |
|---|
| 54 | 'test2.example.org/' => array( 'domain' => 'test2.example.org', 'path' => '/', |
|---|
| 55 | 'meta' => array( 'public' => 1, 'archived' => 0, 'mature' => 1, 'spam' => 0, 'deleted' => 1 ) ), |
|---|
| 56 | 'test3.example.org/zig/' => array( 'domain' => 'test3.example.org', 'path' => '/zig/', |
|---|
| 57 | 'meta' => array( 'public' => 1, 'archived' => 0, 'mature' => 0, 'spam' => 0, 'deleted' => 0 ) ), |
|---|
| 58 | 'atest.example.org/' => array( 'domain' => 'atest.example.org', 'path' => '/', |
|---|
| 59 | 'meta' => array( 'public' => 1, 'archived' => 1, 'mature' => 1, 'spam' => 0, 'deleted' => 0 ) ), |
|---|
| 60 | ); |
|---|
| 61 | |
|---|
| 62 | foreach ( self::$site_ids as &$id ) { |
|---|
| 63 | $id = $factory->blog->create( $id ); |
|---|
| 64 | } |
|---|
| 65 | unset( $id ); |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | public static function wpTearDownAfterClass() { |
|---|
| 69 | foreach ( self::$site_ids as $site_id ) { |
|---|
| 70 | wpmu_delete_blog( $site_id, true ); |
|---|
| 71 | } |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | public function test_ms_sites_list_table_site_status_views() { |
|---|
| 75 | // status public |
|---|
| 76 | $_REQUEST['site_status'] = 'public'; |
|---|
| 77 | |
|---|
| 78 | $this->table->prepare_items(); |
|---|
| 79 | |
|---|
| 80 | $items = wp_list_pluck( $this->table->items, 'blog_id' ); |
|---|
| 81 | $items = array_map( 'intval', $items ); |
|---|
| 82 | |
|---|
| 83 | $this->assertEqualSetsWithMessage( get_sites( array ( 'public' => 1, 'fields' => 'ids', 'number' => 0 ) ), $items, 'site_status=public' ); |
|---|
| 84 | |
|---|
| 85 | // status archived |
|---|
| 86 | $_REQUEST['site_status'] = 'archived'; |
|---|
| 87 | |
|---|
| 88 | $this->table->prepare_items(); |
|---|
| 89 | |
|---|
| 90 | $items = wp_list_pluck( $this->table->items, 'blog_id' ); |
|---|
| 91 | $items = array_map( 'intval', $items ); |
|---|
| 92 | |
|---|
| 93 | $this->assertEqualSetsWithMessage( get_sites( array ( 'archived' => 1, 'fields' => 'ids', 'number' => 0 ) ), $items, 'site_status=archived' ); |
|---|
| 94 | |
|---|
| 95 | // status mature |
|---|
| 96 | $_REQUEST['site_status'] = 'mature'; |
|---|
| 97 | |
|---|
| 98 | $this->table->prepare_items(); |
|---|
| 99 | |
|---|
| 100 | $items = wp_list_pluck( $this->table->items, 'blog_id' ); |
|---|
| 101 | $items = array_map( 'intval', $items ); |
|---|
| 102 | |
|---|
| 103 | $this->assertEqualSetsWithMessage( get_sites( array ( 'mature' => 1, 'fields' => 'ids', 'number' => 0 ) ), $items, 'site_status=mature' ); |
|---|
| 104 | |
|---|
| 105 | // status spam |
|---|
| 106 | $_REQUEST['site_status'] = 'spam'; |
|---|
| 107 | |
|---|
| 108 | $this->table->prepare_items(); |
|---|
| 109 | |
|---|
| 110 | $items = wp_list_pluck( $this->table->items, 'blog_id' ); |
|---|
| 111 | $items = array_map( 'intval', $items ); |
|---|
| 112 | |
|---|
| 113 | $this->assertEqualSetsWithMessage( get_sites( array ( 'spam' => 1, 'fields' => 'ids', 'number' => 0 ) ), $items, 'site_status=spam' ); |
|---|
| 114 | |
|---|
| 115 | // status deleted |
|---|
| 116 | $_REQUEST['site_status'] = 'deleted'; |
|---|
| 117 | |
|---|
| 118 | $this->table->prepare_items(); |
|---|
| 119 | |
|---|
| 120 | $items = wp_list_pluck( $this->table->items, 'blog_id' ); |
|---|
| 121 | $items = array_map( 'intval', $items ); |
|---|
| 122 | |
|---|
| 123 | $this->assertEqualSetsWithMessage( get_sites( array ( 'deleted' => 1, 'fields' => 'ids', 'number' => 0 ) ), $items, 'site_status=deleted' ); |
|---|
| 124 | |
|---|
| 125 | // negative test: status deleted, but checked against all sites (i.e., should fail assertEqualSets()) |
|---|
| 126 | $_REQUEST['site_status'] = 'deleted'; |
|---|
| 127 | |
|---|
| 128 | $this->table->prepare_items(); |
|---|
| 129 | |
|---|
| 130 | $items = wp_list_pluck( $this->table->items, 'blog_id' ); |
|---|
| 131 | $items = array_map( 'intval', $items ); |
|---|
| 132 | |
|---|
| 133 | $this->assertNotEmpty( array_diff( get_sites( array( 'fields' => 'ids', 'number' => 0 ) ), $items), 'site_status=deleted, check against all sites' ); |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | /** |
|---|
| 137 | * Test WP_MS_Sites_List_Table::is_base_request(), which is used to the "All" view should |
|---|
| 138 | * be considered as 'current' |
|---|
| 139 | * |
|---|
| 140 | * As of the writing of this test, there is inconsistency between the various |
|---|
| 141 | * WP_List_Table subclasses that display views in this regard. For instance, |
|---|
| 142 | * WP_(Posts|Plugins)_List_Table show no view as current when a search has been performed, |
|---|
| 143 | * wherea WP_Users_List_Table shows "All" as current IF the search was performed |
|---|
| 144 | * while the user is on the "All" view. The patch in 30871 that adds site_status |
|---|
| 145 | * views aligns with WP_Users_List_Table. |
|---|
| 146 | * |
|---|
| 147 | * @todo open a trac ticket to make ALL subclasses act this way |
|---|
| 148 | */ |
|---|
| 149 | public function test_ms_sites_list_table_is_base_request() { |
|---|
| 150 | // tests that should be true |
|---|
| 151 | $_GET = array(); |
|---|
| 152 | $is_base_request = self::callMethod( $this->table, 'is_base_request'); |
|---|
| 153 | |
|---|
| 154 | $this->assertTrue( $is_base_request, '$_GET=array()' ); |
|---|
| 155 | |
|---|
| 156 | $_GET = array ('site_status' => 'all' ); |
|---|
| 157 | $is_base_request = self::callMethod( $this->table, 'is_base_request'); |
|---|
| 158 | |
|---|
| 159 | $this->assertTrue( $is_base_request, '$_GET=array(site_status=all)' ); |
|---|
| 160 | |
|---|
| 161 | $_GET = array ('s' => 'wordpress'); |
|---|
| 162 | $is_base_request = self::callMethod( $this->table, 'is_base_request'); |
|---|
| 163 | |
|---|
| 164 | $this->assertTrue( $is_base_request, '$_GET=array(s=wordpress)' ); |
|---|
| 165 | |
|---|
| 166 | $_GET = array ('s' => 'wordpress', 'site_status' => 'all' ); |
|---|
| 167 | $is_base_request = self::callMethod( $this->table, 'is_base_request'); |
|---|
| 168 | |
|---|
| 169 | $this->assertTrue( $is_base_request, '$_GET=array(s=wordpress,site_status=all)' ); |
|---|
| 170 | |
|---|
| 171 | // in case a plugin adds query_args core is unware of |
|---|
| 172 | $_GET = array ('foo' => 'bar'); |
|---|
| 173 | $is_base_request = self::callMethod( $this->table, 'is_base_request'); |
|---|
| 174 | |
|---|
| 175 | $this->assertTrue( $is_base_request, '$_GET=array(foo=bar)' ); |
|---|
| 176 | |
|---|
| 177 | // tests that should be false |
|---|
| 178 | $_GET = array('site_status' => 'public'); |
|---|
| 179 | $is_base_request = self::callMethod( $this->table, 'is_base_request'); |
|---|
| 180 | |
|---|
| 181 | $this->assertFalse( $is_base_request, '$_GET=array(site_status=public)' ); |
|---|
| 182 | |
|---|
| 183 | $_GET = array('site_status' => 'public', 'foo' => 'bar'); |
|---|
| 184 | $is_base_request = self::callMethod( $this->table, 'is_base_request'); |
|---|
| 185 | |
|---|
| 186 | $this->assertFalse( $is_base_request, '$_GET=array(site_status=public, foo=bar)' ); |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | /** |
|---|
| 190 | * call a protected/private method so that method can be tested...when doing so is |
|---|
| 191 | * less error prone than testing the output of a public method that relies on it. |
|---|
| 192 | * |
|---|
| 193 | * @param class $obj |
|---|
| 194 | * @param string $method Protected/private method name of $obj |
|---|
| 195 | * @param array $args |
|---|
| 196 | * |
|---|
| 197 | * @return mixed Return value of $method |
|---|
| 198 | * |
|---|
| 199 | * @link http://stackoverflow.com/questions/249664/best-practices-to-test-protected-methods-with-phpunit#answer-8702347 |
|---|
| 200 | */ |
|---|
| 201 | public static function callMethod( $obj, $method, $args = array() ) { |
|---|
| 202 | $class = new \ReflectionClass( $obj ); |
|---|
| 203 | $method = $class->getMethod( $method ); |
|---|
| 204 | $method->setAccessible( true ); |
|---|
| 205 | |
|---|
| 206 | return $method->invokeArgs( $obj, $args ); |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | /** |
|---|
| 210 | * same as assertEqualSets() in phpunit/includes/testcase.php but passes |
|---|
| 211 | * $message thru to assertEquals() so that when multiple asserts are included |
|---|
| 212 | * in a single test the assert that failed can be more easily identified |
|---|
| 213 | * |
|---|
| 214 | * @todo open meta.trac (is that the right place?) ticket suggesting that all |
|---|
| 215 | * of the "custom" asserts in testcase.php accept $message |
|---|
| 216 | */ |
|---|
| 217 | public function assertEqualSetsWithMessage( $expected, $actual, $message = '' ) { |
|---|
| 218 | sort( $expected ); |
|---|
| 219 | sort( $actual ); |
|---|
| 220 | |
|---|
| 221 | $this->assertEquals( $expected, $actual, $message ); |
|---|
| 222 | } |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | endif; |
|---|