Skip to content
Snippets Groups Projects
test-admin-callbacks.php 1.78 KiB
Newer Older
<?php
/**
 * Class AdminCallbacks
 *
 * @package External_Permalinks_Redux
 */

/**
 * Test admin callbacks
 */
class AdminCallbacks extends WP_UnitTestCase {
	/**
	 * Redirect destination.
	 */
	const DESTINATION = 'https://w.org/';

	/**
	 * Redirect type.
	 */
	const TYPE = 302;

	/**
	 * Test post ID.
	 *
	 * @var int
	 */
	protected $post_id;

	/**
	 * Plugin instance.
	 *
	 * @var external_permalinks_redux
	 */
	protected $plugin;

	/**
	 * Metabox nonce.
	 *
	 * @var string
	 */
	protected $nonce;

	/**
	 * Create some objects with redirects.
	 */
Erick Hitter's avatar
Erick Hitter committed
	public function set_up() {
		parent::set_up();

		$this->plugin = external_permalinks_redux::get_instance();

		$this->post_id = $this->factory->post->create(
				'post_type' => 'post',
		);

		$this->nonce = wp_create_nonce( 'external-permalinks-redux' );
	}

	/**
	 * Test metabox rendering.
	 */
	public function test_meta_box() {
		ob_start();
		$this->plugin->meta_box( get_post( $this->post_id ) );
		$meta_box_contents = ob_get_clean();

		$this->assertContains( 'value="' . $this->nonce . '"', $meta_box_contents );

		foreach ( array_keys( $this->plugin->status_codes ) as $code ) {
			$this->assertContains( 'value="' . $code . '"', $meta_box_contents );
		}
	}

	/**
	 * Test metabox save.
	 */
	public function test_save_callback() {
		$_POST[ $this->plugin->meta_key_target . '_nonce' ] = $this->nonce;
		$_POST[ $this->plugin->meta_key_target . '_url' ] = static::DESTINATION;
		$_POST[ $this->plugin->meta_key_target . '_type' ] = static::TYPE;

		$this->plugin->action_save_post( $this->post_id );

		$this->assertEquals( static::DESTINATION, get_post_meta( $this->post_id, $this->plugin->meta_key_target, true ) );
		$this->assertEquals( static::TYPE, get_post_meta( $this->post_id, $this->plugin->meta_key_type, true ) );
	}
}