test-ui.php 3.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
<?php
/**
 * Test UI methods.
 *
 * @package WP_Revisions_Control
 */

/**
 * Class TestUI.
 */
class TestUI extends WP_UnitTestCase {
	/**
	 * Plugin slug used in many settings etc.
	 *
	 * @var string
	 */
	protected static $settings_section = 'wp_revisions_control';

	/**
	 * Plugin's limit meta key.
	 *
	 * @var string
	 */
	protected static $meta_key = '_wp_rev_ctl_limit';

	/**
	 * Test meta box with no meta set.
	 */
	public function test_no_meta() {
		$post_id    = $this->factory->post->create();

		ob_start();
		WP_Revisions_Control::get_instance()->revisions_meta_box( get_post( $post_id ) );
		$meta_box = ob_get_clean();

		$this->assertContains(
			'value=""',
			$meta_box,
			'Failed to assert that meta box has no value when no setting exists.'
		);
	}

	/**
	 * Test meta box with no limit set.
	 */
	public function test_no_limit() {
		$post_id    = $this->factory->post->create();
		update_post_meta( $post_id, static::$meta_key, -1 );

		ob_start();
		WP_Revisions_Control::get_instance()->revisions_meta_box( get_post( $post_id ) );
		$meta_box = ob_get_clean();

		$this->assertContains(
			'value=""',
			$meta_box,
			'Failed to assert that meta box has no value when no limit exists.'
		);
	}

	/**
	 * Test settings-field output when no options are set.
	 */
	public function test_settings_fields_no_options() {
		ob_start();
		WP_Revisions_Control::get_instance()->field_post_type( array( 'post_type' => 'post' ) );
		$post_field = ob_get_clean();

		ob_start();
		WP_Revisions_Control::get_instance()->field_post_type( array( 'post_type' => 'page' ) );
		$page_field = ob_get_clean();

		$name_format  = 'name="%1$s[%2$s]"';
		$value_format = 'value=""';

		$this->assertContains(
			sprintf(
				$name_format,
				static::$settings_section,
				'post'
			),
			$post_field,
			'Failed to assert that post field had correct name for post type.'
		);

		$this->assertContains(
			sprintf(
				$name_format,
				static::$settings_section,
				'page'
			),
			$page_field,
			'Failed to assert that page field had correct name for post type.'
		);

		$this->assertContains(
			$value_format,
			$post_field,
			'Failed to assert that post field had correct value.'
		);

		$this->assertContains(
			$value_format,
			$page_field,
			'Failed to assert that page field had correct value.'
		);
	}

	/**
	 * Test settings-field output when options are set.
	 */
	public function test_settings_fields_with_options() {
		$value = 12;

		update_option(
			static::$settings_section,
			[
				'post' => $value,
			]
		);

		ob_start();
		WP_Revisions_Control::get_instance()->field_post_type( array( 'post_type' => 'post' ) );
		$post_field = ob_get_clean();

		$name_format  = 'name="%1$s[%2$s]"';
		$value_format = 'value="%1$s"';

		$this->assertContains(
			sprintf(
				$name_format,
				static::$settings_section,
				'post'
			),
			$post_field,
			'Failed to assert that post field had correct name for post type.'
		);

		$this->assertContains(
			sprintf(
				$value_format,
				$value
			),
			$post_field,
			'Failed to assert that post field had correct value.'
		);
	}

	/**
	 * Test settings-field output when options are set.
	 */
	public function test_settings_fields_with_options_keep_all() {
		$value = -1;

		update_option(
			static::$settings_section,
			[
				'post' => $value,
			]
		);

		ob_start();
		WP_Revisions_Control::get_instance()->field_post_type( array( 'post_type' => 'post' ) );
		$post_field = ob_get_clean();

		$name_format  = 'name="%1$s[%2$s]"';
		$value_format = 'value=""';

		$this->assertContains(
			sprintf(
				$name_format,
				static::$settings_section,
				'post'
			),
			$post_field,
			'Failed to assert that post field had correct name for post type.'
		);

		$this->assertContains(
			sprintf(
				$value_format,
				$value
			),
			$post_field,
			'Failed to assert that post field had correct value.'
		);
	}
}