|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Tests for the empty results and table navigation of WP_Plugin_Install_List_Table. |
| 5 | + * |
| 6 | + * @group admin |
| 7 | + * @group plugins |
| 8 | + */ |
| 9 | +class Tests_Admin_wpPluginInstallListTableNoItems extends WP_UnitTestCase { |
| 10 | + |
| 11 | + /** |
| 12 | + * The original tab global. |
| 13 | + * |
| 14 | + * @var mixed |
| 15 | + */ |
| 16 | + private $original_tab; |
| 17 | + |
| 18 | + /** |
| 19 | + * The original request URI, or null when it was not set. |
| 20 | + * |
| 21 | + * @var string|null |
| 22 | + */ |
| 23 | + private $original_request_uri; |
| 24 | + |
| 25 | + /** |
| 26 | + * Saves the tab global and the request URI. |
| 27 | + */ |
| 28 | + public function set_up() { |
| 29 | + parent::set_up(); |
| 30 | + |
| 31 | + $this->original_tab = $GLOBALS['tab'] ?? null; |
| 32 | + $this->original_request_uri = $_SERVER['REQUEST_URI'] ?? null; |
| 33 | + |
| 34 | + $_SERVER['REQUEST_URI'] = '/wp-admin/plugin-install.php?tab=search&s=forms'; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Restores the tab global and the request URI. |
| 39 | + */ |
| 40 | + public function tear_down() { |
| 41 | + $GLOBALS['tab'] = $this->original_tab; |
| 42 | + |
| 43 | + if ( null === $this->original_request_uri ) { |
| 44 | + unset( $_SERVER['REQUEST_URI'] ); |
| 45 | + } else { |
| 46 | + $_SERVER['REQUEST_URI'] = $this->original_request_uri; |
| 47 | + } |
| 48 | + |
| 49 | + parent::tear_down(); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Tests the message when a search has no results. |
| 54 | + * |
| 55 | + * @ticket 65819 |
| 56 | + * |
| 57 | + * @covers WP_Plugin_Install_List_Table::no_items |
| 58 | + */ |
| 59 | + public function test_no_items_should_suggest_a_different_search() { |
| 60 | + $this->assertSame( |
| 61 | + '<div class="no-plugin-results">No plugins found. Try a different search.</div>', |
| 62 | + trim( get_echo( array( $this->get_table(), 'no_items' ) ) ) |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Tests that no table navigation is shown on the Featured tab. |
| 68 | + * |
| 69 | + * @ticket 65819 |
| 70 | + * |
| 71 | + * @covers WP_Plugin_Install_List_Table::display_tablenav |
| 72 | + * |
| 73 | + * @dataProvider data_positions |
| 74 | + * |
| 75 | + * @param string $which The navigation position. |
| 76 | + */ |
| 77 | + public function test_display_tablenav_should_output_nothing_on_featured_tab( $which ) { |
| 78 | + $GLOBALS['tab'] = 'featured'; |
| 79 | + |
| 80 | + $this->assertSame( '', $this->get_tablenav( $which ) ); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Data provider. |
| 85 | + * |
| 86 | + * @return array[] |
| 87 | + */ |
| 88 | + public static function data_positions() { |
| 89 | + return array( |
| 90 | + 'top' => array( 'top' ), |
| 91 | + 'bottom' => array( 'bottom' ), |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Tests the top navigation, with the referer field and the table header action. |
| 97 | + * |
| 98 | + * @ticket 65819 |
| 99 | + * |
| 100 | + * @covers WP_Plugin_Install_List_Table::display_tablenav |
| 101 | + */ |
| 102 | + public function test_display_tablenav_should_output_top_navigation() { |
| 103 | + $GLOBALS['tab'] = 'search'; |
| 104 | + |
| 105 | + $action = new MockAction(); |
| 106 | + add_action( 'install_plugins_table_header', array( $action, 'action' ) ); |
| 107 | + add_action( |
| 108 | + 'install_plugins_table_header', |
| 109 | + static function () { |
| 110 | + echo '<span>Header</span>'; |
| 111 | + } |
| 112 | + ); |
| 113 | + |
| 114 | + $this->assertSame( |
| 115 | + '<input type="hidden" name="_wp_http_referer" value="/wp-admin/plugin-install.php?tab=search&s=forms" />' |
| 116 | + . '<div class="tablenav top"><div class="alignleft actions"><span>Header</span></div>' |
| 117 | + . '<div class="tablenav-pages no-pages"><span class="displaying-num">0 items</span></div><br class="clear" /></div>', |
| 118 | + $this->get_tablenav( 'top' ), |
| 119 | + 'The top navigation should include the referer field and the header.' |
| 120 | + ); |
| 121 | + $this->assertSame( 1, $action->get_call_count(), 'The table header action should fire once.' ); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Tests the bottom navigation, without the table header action. |
| 126 | + * |
| 127 | + * @ticket 65819 |
| 128 | + * |
| 129 | + * @covers WP_Plugin_Install_List_Table::display_tablenav |
| 130 | + */ |
| 131 | + public function test_display_tablenav_should_output_bottom_navigation() { |
| 132 | + $GLOBALS['tab'] = 'search'; |
| 133 | + |
| 134 | + $action = new MockAction(); |
| 135 | + add_action( 'install_plugins_table_header', array( $action, 'action' ) ); |
| 136 | + |
| 137 | + $this->assertSame( '<div class="tablenav bottom"><div class="tablenav-pages no-pages"><span class="displaying-num">0 items</span></div><br class="clear" /></div>', $this->get_tablenav( 'bottom' ), 'The bottom navigation should be output.' ); |
| 138 | + $this->assertSame( 0, $action->get_call_count(), 'The table header action should not fire.' ); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Returns an Add Plugins list table. |
| 143 | + * |
| 144 | + * @return WP_Plugin_Install_List_Table The list table. |
| 145 | + */ |
| 146 | + private function get_table() { |
| 147 | + return _get_list_table( 'WP_Plugin_Install_List_Table', array( 'screen' => 'plugin-install' ) ); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Returns the table navigation markup, without the whitespace between tags. |
| 152 | + * |
| 153 | + * @param string $which The navigation position. |
| 154 | + * @return string The markup. |
| 155 | + */ |
| 156 | + private function get_tablenav( $which ) { |
| 157 | + $table = $this->get_table(); |
| 158 | + $method = new ReflectionMethod( $table, 'display_tablenav' ); |
| 159 | + if ( PHP_VERSION_ID < 80100 ) { |
| 160 | + $method->setAccessible( true ); |
| 161 | + } |
| 162 | + |
| 163 | + $output = get_echo( array( $method, 'invoke' ), array( $table, $which ) ); |
| 164 | + |
| 165 | + return preg_replace( '/>\s+</', '><', trim( $output ) ); |
| 166 | + } |
| 167 | +} |
0 commit comments