| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Admin ajax functions to be tested |
| 5 | */ |
| 6 | require_once( ABSPATH . 'wp-admin/includes/ajax-actions.php' ); |
| 7 | |
| 8 | /** |
| 9 | * Testing ajax tag search functionality |
| 10 | * |
| 11 | * @package WordPress |
| 12 | * @subpackage UnitTests |
| 13 | * @since 3.4.0 |
| 14 | * @group ajax |
| 15 | */ |
| 16 | class Tests_Ajax_TagSearch extends WP_Ajax_UnitTestCase { |
| 17 | |
| 18 | /** |
| 19 | * List of terms to insert on setup |
| 20 | * |
| 21 | * @var array |
| 22 | */ |
| 23 | private static $terms = array( |
| 24 | 'chattels', |
| 25 | 'depo', |
| 26 | 'energumen', |
| 27 | 'figuriste', |
| 28 | 'habergeon', |
| 29 | 'impropriation', |
| 30 | ); |
| 31 | |
| 32 | private static $term_ids = array(); |
| 33 | |
| 34 | public static function wpSetUpBeforeClass() { |
| 35 | foreach ( self::$terms as $t ) { |
| 36 | self::$term_ids[] = wp_insert_term( $t, 'post_tag' ); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Test as an admin |
| 42 | * |
| 43 | * @expectedDeprecated wp_ajax_ajax_tag_search |
| 44 | */ |
| 45 | public function test_post_tag() { |
| 46 | |
| 47 | // Become an administrator |
| 48 | $this->_setRole( 'administrator' ); |
| 49 | |
| 50 | // Set up a default request |
| 51 | $_GET['tax'] = 'post_tag'; |
| 52 | $_GET['q'] = 'chat'; |
| 53 | |
| 54 | // Make the request |
| 55 | try { |
| 56 | $this->_handleAjax( 'ajax-tag-search' ); |
| 57 | } catch ( WPAjaxDieContinueException $e ) { |
| 58 | unset( $e ); |
| 59 | } |
| 60 | |
| 61 | // Ensure we found the right match |
| 62 | $this->assertEquals( $this->_last_response, 'chattels' ); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Test with no results |
| 67 | * |
| 68 | * @expectedDeprecated wp_ajax_ajax_tag_search |
| 69 | */ |
| 70 | public function test_no_results() { |
| 71 | |
| 72 | // Become an administrator |
| 73 | $this->_setRole( 'administrator' ); |
| 74 | |
| 75 | // Set up a default request |
| 76 | $_GET['tax'] = 'post_tag'; |
| 77 | $_GET['q'] = md5( uniqid() ); |
| 78 | |
| 79 | // Make the request |
| 80 | // No output, so we get a stop exception |
| 81 | $this->setExpectedException( 'WPAjaxDieStopException', '' ); |
| 82 | $this->_handleAjax( 'ajax-tag-search' ); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Test with commas |
| 87 | * |
| 88 | * @expectedDeprecated wp_ajax_ajax_tag_search |
| 89 | */ |
| 90 | public function test_with_comma() { |
| 91 | |
| 92 | // Become an administrator |
| 93 | $this->_setRole( 'administrator' ); |
| 94 | |
| 95 | // Set up a default request |
| 96 | $_GET['tax'] = 'post_tag'; |
| 97 | $_GET['q'] = 'some,nonsense, terms,chat'; // Only the last term in the list is searched |
| 98 | |
| 99 | // Make the request |
| 100 | try { |
| 101 | $this->_handleAjax( 'ajax-tag-search' ); |
| 102 | } catch ( WPAjaxDieContinueException $e ) { |
| 103 | unset( $e ); |
| 104 | } |
| 105 | |
| 106 | // Ensure we found the right match |
| 107 | $this->assertEquals( $this->_last_response, 'chattels' ); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Test as a logged out user |
| 112 | * |
| 113 | * @expectedDeprecated wp_ajax_ajax_tag_search |
| 114 | */ |
| 115 | public function test_logged_out() { |
| 116 | |
| 117 | // Log out |
| 118 | wp_logout(); |
| 119 | |
| 120 | // Set up a default request |
| 121 | $_GET['tax'] = 'post_tag'; |
| 122 | $_GET['q'] = 'chat'; |
| 123 | |
| 124 | // Make the request |
| 125 | $this->setExpectedException( 'WPAjaxDieStopException', '-1' ); |
| 126 | $this->_handleAjax( 'ajax-tag-search' ); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Test with an invalid taxonomy type |
| 131 | * |
| 132 | * @expectedDeprecated wp_ajax_ajax_tag_search |
| 133 | */ |
| 134 | public function test_invalid_tax() { |
| 135 | |
| 136 | // Become an administrator |
| 137 | $this->_setRole( 'administrator' ); |
| 138 | |
| 139 | // Set up a default request |
| 140 | $_GET['tax'] = 'invalid-taxonomy'; |
| 141 | $_GET['q'] = 'chat'; |
| 142 | |
| 143 | // Make the request |
| 144 | $this->setExpectedException( 'WPAjaxDieStopException', '0' ); |
| 145 | $this->_handleAjax( 'ajax-tag-search' ); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Test as an unprivileged user |
| 150 | * |
| 151 | * @expectedDeprecated wp_ajax_ajax_tag_search |
| 152 | */ |
| 153 | public function test_unprivileged_user() { |
| 154 | |
| 155 | // Become an administrator |
| 156 | $this->_setRole( 'subscriber' ); |
| 157 | |
| 158 | // Set up a default request |
| 159 | $_GET['tax'] = 'post_tag'; |
| 160 | $_GET['q'] = 'chat'; |
| 161 | |
| 162 | // Make the request |
| 163 | $this->setExpectedException( 'WPAjaxDieStopException', '-1' ); |
| 164 | $this->_handleAjax( 'ajax-tag-search' ); |
| 165 | } |
| 166 | |
| 167 | } |