| 1 | <?php
|
|---|
| 2 | /**
|
|---|
| 3 | * Ajax tests for adding tags.
|
|---|
| 4 | *
|
|---|
| 5 | * @package WordPress
|
|---|
| 6 | * @subpackage UnitTests
|
|---|
| 7 | */
|
|---|
| 8 |
|
|---|
| 9 | /**
|
|---|
| 10 | * Admin ajax functions to be tested.
|
|---|
| 11 | */
|
|---|
| 12 | require_once ABSPATH . 'wp-admin/includes/ajax-actions.php';
|
|---|
| 13 |
|
|---|
| 14 | /**
|
|---|
| 15 | * Class for testing ajax add tag functionality.
|
|---|
| 16 | *
|
|---|
| 17 | * @since 5.9.0
|
|---|
| 18 | * @group ajax
|
|---|
| 19 | */
|
|---|
| 20 | class Tests_Ajax_AddTag extends WP_Ajax_UnitTestCase {
|
|---|
| 21 | /**
|
|---|
| 22 | * List of terms to insert on setup
|
|---|
| 23 | *
|
|---|
| 24 | * @var array
|
|---|
| 25 | */
|
|---|
| 26 | private static $terms = array(
|
|---|
| 27 | 'jazz',
|
|---|
| 28 | );
|
|---|
| 29 |
|
|---|
| 30 | /**
|
|---|
| 31 | * Set up before class.
|
|---|
| 32 | */
|
|---|
| 33 | public static function wpSetUpBeforeClass() {
|
|---|
| 34 | foreach ( self::$terms as $term ) {
|
|---|
| 35 | wp_insert_term( $term, 'category' );
|
|---|
| 36 | }
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | /**
|
|---|
| 40 | * Test adding a category.
|
|---|
| 41 | */
|
|---|
| 42 | public function test_add_category() {
|
|---|
| 43 |
|
|---|
| 44 | $this->_setRole( 'administrator' );
|
|---|
| 45 |
|
|---|
| 46 | $_POST['taxonomy'] = 'category';
|
|---|
| 47 | $_POST['post_type'] = 'post';
|
|---|
| 48 | $_POST['screen'] = 'edit-category';
|
|---|
| 49 | $_POST['action'] = 'add-tag';
|
|---|
| 50 | $_POST['tag-name'] = 'blues';
|
|---|
| 51 | $_POST['_wpnonce_add-tag'] = wp_create_nonce( 'add-tag' );
|
|---|
| 52 |
|
|---|
| 53 | try {
|
|---|
| 54 | $this->_handleAjax( 'add-tag' );
|
|---|
| 55 | } catch ( WPAjaxDieContinueException $e ) {
|
|---|
| 56 | unset( $e );
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | $xml = simplexml_load_string( $this->_last_response, 'SimpleXMLElement', LIBXML_NOCDATA );
|
|---|
| 60 |
|
|---|
| 61 | $expected = 'Category added.';
|
|---|
| 62 | $actual = (string) $xml->response->taxonomy->response_data;
|
|---|
| 63 |
|
|---|
| 64 | $this->assertSame( $expected, $actual );
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | /**
|
|---|
| 68 | * Test adding a post tag.
|
|---|
| 69 | */
|
|---|
| 70 | public function test_add_post_tag() {
|
|---|
| 71 |
|
|---|
| 72 | $this->_setRole( 'administrator' );
|
|---|
| 73 |
|
|---|
| 74 | $_POST['taxonomy'] = 'post_tag';
|
|---|
| 75 | $_POST['post_type'] = 'post';
|
|---|
| 76 | $_POST['screen'] = 'edit-post_tag';
|
|---|
| 77 | $_POST['action'] = 'add-tag';
|
|---|
| 78 | $_POST['tag-name'] = 'Louis Armstrong';
|
|---|
| 79 | $_POST['_wpnonce_add-tag'] = wp_create_nonce( 'add-tag' );
|
|---|
| 80 |
|
|---|
| 81 | try {
|
|---|
| 82 | $this->_handleAjax( 'add-tag' );
|
|---|
| 83 | } catch ( WPAjaxDieContinueException $e ) {
|
|---|
| 84 | unset( $e );
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | $xml = simplexml_load_string( $this->_last_response, 'SimpleXMLElement', LIBXML_NOCDATA );
|
|---|
| 88 |
|
|---|
| 89 | $expected = 'Tag added.';
|
|---|
| 90 | $actual = (string) $xml->response->taxonomy->response_data;
|
|---|
| 91 |
|
|---|
| 92 | $this->assertSame( $expected, $actual );
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | /**
|
|---|
| 96 | * Test that adding a category should work with message filtering.
|
|---|
| 97 | */
|
|---|
| 98 | public function test_add_category_should_work_with_message_filtering() {
|
|---|
| 99 |
|
|---|
| 100 | $this->_setRole( 'administrator' );
|
|---|
| 101 |
|
|---|
| 102 | $_POST['taxonomy'] = 'category';
|
|---|
| 103 | $_POST['post_type'] = 'post';
|
|---|
| 104 | $_POST['screen'] = 'edit-category';
|
|---|
| 105 | $_POST['action'] = 'add-tag';
|
|---|
| 106 | $_POST['tag-name'] = 'techno';
|
|---|
| 107 | $_POST['_wpnonce_add-tag'] = wp_create_nonce( 'add-tag' );
|
|---|
| 108 |
|
|---|
| 109 | try {
|
|---|
| 110 | add_filter( 'term_updated_messages', array( $this, 'message_filter' ) );
|
|---|
| 111 | $this->_handleAjax( 'add-tag' );
|
|---|
| 112 | remove_filter( 'term_updated_messages', array( $this, 'message_filter' ) );
|
|---|
| 113 | } catch ( WPAjaxDieContinueException $e ) {
|
|---|
| 114 | unset( $e );
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | $xml = simplexml_load_string( $this->_last_response, 'SimpleXMLElement', LIBXML_NOCDATA );
|
|---|
| 118 |
|
|---|
| 119 | $expected = 'New category added.';
|
|---|
| 120 | $actual = (string) $xml->response->taxonomy->response_data;
|
|---|
| 121 |
|
|---|
| 122 | $this->assertSame( $expected, $actual );
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | /**
|
|---|
| 126 | * Filters the messages displayed when a tag is updated.
|
|---|
| 127 | *
|
|---|
| 128 | * @param array $messages The messages to be displayed.
|
|---|
| 129 | * @return array $messages The messages to be displayed.
|
|---|
| 130 | */
|
|---|
| 131 | public function message_filter( $messages ) {
|
|---|
| 132 | if ( isset( $messages['category'][1] ) ) {
|
|---|
| 133 | $messages['category'][1] = 'New category added.';
|
|---|
| 134 | }
|
|---|
| 135 | return $messages;
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | /**
|
|---|
| 139 | * Test that adding a category without capability should error.
|
|---|
| 140 | */
|
|---|
| 141 | public function test_adding_category_without_capability_should_error() {
|
|---|
| 142 |
|
|---|
| 143 | $this->_setRole( 'subscriber' );
|
|---|
| 144 |
|
|---|
| 145 | $_POST['taxonomy'] = 'category';
|
|---|
| 146 | $_POST['post_type'] = 'post';
|
|---|
| 147 | $_POST['screen'] = 'edit-category';
|
|---|
| 148 | $_POST['action'] = 'add-tag';
|
|---|
| 149 | $_POST['tag-name'] = 'disco';
|
|---|
| 150 | $_POST['_wpnonce_add-tag'] = wp_create_nonce( 'add-tag' );
|
|---|
| 151 |
|
|---|
| 152 | $this->setExpectedException( 'WPAjaxDieStopException', '-1' );
|
|---|
| 153 |
|
|---|
| 154 | try {
|
|---|
| 155 | $this->_handleAjax( 'add-tag' );
|
|---|
| 156 | } catch ( WPAjaxDieContinueException $e ) {
|
|---|
| 157 | unset( $e );
|
|---|
| 158 | }
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | /**
|
|---|
| 162 | * Test that adding an existing category should error.
|
|---|
| 163 | */
|
|---|
| 164 | public function test_adding_existing_category_should_error() {
|
|---|
| 165 |
|
|---|
| 166 | $this->_setRole( 'administrator' );
|
|---|
| 167 |
|
|---|
| 168 | $_POST['taxonomy'] = 'category';
|
|---|
| 169 | $_POST['post_type'] = 'post';
|
|---|
| 170 | $_POST['screen'] = 'edit-category';
|
|---|
| 171 | $_POST['action'] = 'add-tag';
|
|---|
| 172 | $_POST['tag-name'] = self::$terms[0];
|
|---|
| 173 | $_POST['_wpnonce_add-tag'] = wp_create_nonce( 'add-tag' );
|
|---|
| 174 |
|
|---|
| 175 | try {
|
|---|
| 176 | $this->_handleAjax( 'add-tag' );
|
|---|
| 177 | } catch ( WPAjaxDieContinueException $e ) {
|
|---|
| 178 | unset( $e );
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | $xml = simplexml_load_string( $this->_last_response, 'SimpleXMLElement', LIBXML_NOCDATA );
|
|---|
| 182 |
|
|---|
| 183 | $expected = 'A term with the name provided already exists with this parent.';
|
|---|
| 184 | $actual = (string) $xml->response->taxonomy->wp_error;
|
|---|
| 185 |
|
|---|
| 186 | $this->assertSame( $expected, $actual );
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | }
|
|---|