<?php
/**
 * Ajax tests for adding tags.
 *
 * @package    WordPress
 * @subpackage UnitTests
 */

/**
 * Admin ajax functions to be tested.
 */
require_once ABSPATH . 'wp-admin/includes/ajax-actions.php';

/**
 * Class for testing ajax add tag functionality.
 *
 * @since      5.9.0
 * @group      ajax
 */
class Tests_Ajax_AddTag extends WP_Ajax_UnitTestCase {
	/**
	 * List of terms to insert on setup
	 *
	 * @var array
	 */
	private static $terms = array(
		'jazz',
	);

	/**
	 * Set up before class.
	 */
	public static function wpSetUpBeforeClass() {
		foreach ( self::$terms as $term ) {
			wp_insert_term( $term, 'category' );
		}
	}

	/**
	 * Test adding a category.
	 */
	public function test_add_category() {

		$this->_setRole( 'administrator' );

		$_POST['taxonomy']         = 'category';
		$_POST['post_type']        = 'post';
		$_POST['screen']           = 'edit-category';
		$_POST['action']           = 'add-tag';
		$_POST['tag-name']         = 'blues';
		$_POST['_wpnonce_add-tag'] = wp_create_nonce( 'add-tag' );

		try {
			$this->_handleAjax( 'add-tag' );
		} catch ( WPAjaxDieContinueException $e ) {
			unset( $e );
		}

		$xml = simplexml_load_string( $this->_last_response, 'SimpleXMLElement', LIBXML_NOCDATA );

		$expected = 'Category added.';
		$actual   = (string) $xml->response->taxonomy->response_data;

		$this->assertSame( $expected, $actual );
	}

	/**
	 * Test adding a post tag.
	 */
	public function test_add_post_tag() {

		$this->_setRole( 'administrator' );

		$_POST['taxonomy']         = 'post_tag';
		$_POST['post_type']        = 'post';
		$_POST['screen']           = 'edit-post_tag';
		$_POST['action']           = 'add-tag';
		$_POST['tag-name']         = 'Louis Armstrong';
		$_POST['_wpnonce_add-tag'] = wp_create_nonce( 'add-tag' );

		try {
			$this->_handleAjax( 'add-tag' );
		} catch ( WPAjaxDieContinueException $e ) {
			unset( $e );
		}

		$xml = simplexml_load_string( $this->_last_response, 'SimpleXMLElement', LIBXML_NOCDATA );

		$expected = 'Tag added.';
		$actual   = (string) $xml->response->taxonomy->response_data;

		$this->assertSame( $expected, $actual );
	}

	/**
	 * Test that adding a category should work with message filtering.
	 */
	public function test_add_category_should_work_with_message_filtering() {

		$this->_setRole( 'administrator' );

		$_POST['taxonomy']         = 'category';
		$_POST['post_type']        = 'post';
		$_POST['screen']           = 'edit-category';
		$_POST['action']           = 'add-tag';
		$_POST['tag-name']         = 'techno';
		$_POST['_wpnonce_add-tag'] = wp_create_nonce( 'add-tag' );

		try {
			add_filter( 'term_updated_messages', array( $this, 'message_filter' ) );
			$this->_handleAjax( 'add-tag' );
			remove_filter( 'term_updated_messages', array( $this, 'message_filter' ) );
		} catch ( WPAjaxDieContinueException $e ) {
			unset( $e );
		}

		$xml = simplexml_load_string( $this->_last_response, 'SimpleXMLElement', LIBXML_NOCDATA );

		$expected = 'New category added.';
		$actual   = (string) $xml->response->taxonomy->response_data;

		$this->assertSame( $expected, $actual );
	}

	/**
	 * Filters the messages displayed when a tag is updated.
	 *
	 * @param  array $messages The messages to be displayed.
	 * @return array $messages The messages to be displayed.
	 */
	public function message_filter( $messages ) {
		if ( isset( $messages['category'][1] ) ) {
			$messages['category'][1] = 'New category added.';
		}
		return $messages;
	}

	/**
	 * Test that adding a category without capability should error.
	 */
	public function test_adding_category_without_capability_should_error() {

		$this->_setRole( 'subscriber' );

		$_POST['taxonomy']         = 'category';
		$_POST['post_type']        = 'post';
		$_POST['screen']           = 'edit-category';
		$_POST['action']           = 'add-tag';
		$_POST['tag-name']         = 'disco';
		$_POST['_wpnonce_add-tag'] = wp_create_nonce( 'add-tag' );

		$this->setExpectedException( 'WPAjaxDieStopException', '-1' );

		try {
			$this->_handleAjax( 'add-tag' );
		} catch ( WPAjaxDieContinueException $e ) {
			unset( $e );
		}
	}

	/**
	 * Test that adding an existing category should error.
	 */
	public function test_adding_existing_category_should_error() {

		$this->_setRole( 'administrator' );

		$_POST['taxonomy']         = 'category';
		$_POST['post_type']        = 'post';
		$_POST['screen']           = 'edit-category';
		$_POST['action']           = 'add-tag';
		$_POST['tag-name']         = self::$terms[0];
		$_POST['_wpnonce_add-tag'] = wp_create_nonce( 'add-tag' );

		try {
			$this->_handleAjax( 'add-tag' );
		} catch ( WPAjaxDieContinueException $e ) {
			unset( $e );
		}

		$xml = simplexml_load_string( $this->_last_response, 'SimpleXMLElement', LIBXML_NOCDATA );

		$expected = 'A term with the name provided already exists with this parent.';
		$actual   = (string) $xml->response->taxonomy->wp_error;

		$this->assertSame( $expected, $actual );
	}

}
