<commit_before><?php

/**
 * @group admin
 */
class Papi_Admin_Meta_Handler_Test extends WP_UnitTestCase {

	public function setUp() {
		parent::setUp();

		$this->handler = new Papi_Admin_Meta_Handler;

		add_filter( 'papi/settings/directories', function () {
			return [1,  PAPI_FIXTURE_DIR . '/page-types', PAPI_FIXTURE_DIR . '/taxonomy-types'];
		} );

		$this->term_id = $this->factory->term->create();
		$this->post_id = $this->factory->post->create();

		$_GET = [];
		$_GET['post'] = $this->post_id;

		update_post_meta( $this->post_id, papi_get_page_type_key(), 'properties-page-type' );
		update_term_meta( $this->term_id, papi_get_page_type_key(), 'properties-taxonomy-type' );

		$this->page_type       = papi_get_entry_type_by_id( 'properties-page-type' );
		$this->extra_page_type = papi_get_entry_type_by_id( 'extra-page-type' );
		$this->taxonomy_type   = papi_get_entry_type_by_id( 'properties-taxonomy-type' );
	}

	public function tearDown() {
		parent::tearDown();
		unset(
			$_GET,
			$_POST,
			$this->handler,
			$this->post_id,
			$this->term_id,
			$this->page_type,
			$this->extra_page_type,
			$this->taxonomy_type
		);
	}

	public function test_actions() {
		$this->assertSame( 1, has_action( 'save_post', [$this->handler, 'save_meta_boxes'] ) );
		$this->assertSame( 1, has_action( 'created_term', [$this->handler, 'save_meta_boxes'] ) );
		$this->assertSame( 1, has_action( 'edit_term', [$this->handler, 'save_meta_boxes'] ) );
	}

	public function test_save_meta_boxes() {
		$property = $this->page_type->get_property( 'string_test' );

		$_POST = papi_test_create_property_post_data( [
			'slug'  => $property->slug,
			'type'  => $property,
			'value' => 'Hello, world!'
		], $_POST );

		$user_id = $this->factory->user->create( [ 'role' => 'administrator' ] );
		wp_set_current_user( $user_id );

		$_POST['papi_meta_nonce'] = wp_create_nonce( 'papi_save_data' );
		$_POST['post_ID'] = $this->post_id;

		$this->handler->save_meta_boxes( $this->post_id, get_post( $this->post_id ) );
		wp_set_current_user( 0 );

		$this->assertSame( 'Hello, world!', papi_get_field( $this->post_id, $property->slug ) );
	}

	public function test_save_meta_boxes_2() {
		$property = $this->page_type->get_property( 'bool_test' );

		$_POST = papi_test_create_property_post_data( [
			'slug'  => $property->slug,
			'type'  => $property,
			'value' => 'on'
		], $_POST );

		$user_id = $this->factory->user->create( [ 'role' => 'administrator' ] );
		wp_set_current_user( $user_id );

		$_POST['papi_meta_nonce'] = wp_create_nonce( 'papi_save_data' );
		$_POST['post_ID'] = $this->post_id;

		$this->handler->save_meta_boxes( $this->post_id, get_post( $this->post_id ) );
		wp_set_current_user( 0 );

		$this->assertTrue( papi_get_field( $this->post_id, $property->slug ) );
	}

	public function test_save_meta_boxes_3() {
		$post_id  = $this->factory->post->create();
		$revs_id  = wp_save_post_revision( $post_id );
		$property = $this->page_type->get_property( 'string_test' );

		$_POST = papi_test_create_property_post_data( [
			'slug'  => $property->slug,
			'type'  => $property,
			'value' => 'Hello, world!'
		], $_POST );

		$user_id = $this->factory->user->create( [ 'role' => 'administrator' ] );
		wp_set_current_user( $user_id );

		$_POST['papi_meta_nonce'] = wp_create_nonce( 'papi_save_data' );
		$_POST['post_ID'] = $revs_id;

		$this->handler->save_meta_boxes( $revs_id, get_post( $revs_id ) );
		wp_set_current_user( 0 );

		$this->assertSame( 'Hello, world!', get_post_meta( $revs_id, unpapify( $property->slug ), true ) );
	}

	/**
	 * Autosave.
	 */
	public function test_save_meta_boxes_4() {
		$post_id  = $this->factory->post->create();
		$property = $this->page_type->get_property( 'string_test' );
		$revs_id  = wp_save_post_revision( $post_id );
		$_POST = papi_test_create_property_post_data( [
			'slug'  => $property->slug,
			'type'  => $property,
			'value' => 'Hello, world!'
		], $_POST );

		$user_id = $this->factory->user->create( [ 'role' => 'administrator' ] );
		wp_set_current_user( $user_id );

		$_POST['papi_meta_nonce'] = wp_create_nonce( 'papi_save_data' );
		$_POST['data'] = ['wp_autosave' => ['post_id' => $revs_id]];

		$this->handler->save_meta_boxes( $post_id, get_post( $post_id ) );
		wp_set_current_user( 0 );

		$this->assertSame( 'Hello, world!', get_post_meta( $post_id, unpapify( $property->slug ), true ) );
	}

	/**
	 * @issue 126
	 */
	public function test_save_meta_boxes_apostrophe() {
		$property = $this->page_type->get_property( 'string_test' );

		$_POST = papi_test_create_property_post_data( [
			'slug'  => $property->slug,
			'type'  => $property,
			'value' => "I'm home"
		], $_POST );

		$user_id = $this->factory->user->create( [ 'role' => 'administrator' ] );
		wp_set_current_user( $user_id );

		$_POST['papi_meta_nonce'] = wp_create_nonce( 'papi_save_data' );
		$_POST['post_ID'] = $this->post_id;

		$this->handler->save_meta_boxes( $this->post_id, get_post( $this->post_id ) );
		wp_set_current_user( 0 );

		$this->assertSame( "I'm home", papi_get_field( $this->post_id, $property->slug ) );
	}
<commit_msg>Add test for autosave draft<commit_after><?php

/**
 * @group admin
 */
class Papi_Admin_Meta_Handler_Test extends WP_UnitTestCase {

	public function setUp() {
		parent::setUp();

		$this->handler = new Papi_Admin_Meta_Handler;

		add_filter( 'papi/settings/directories', function () {
			return [1,  PAPI_FIXTURE_DIR . '/page-types', PAPI_FIXTURE_DIR . '/taxonomy-types'];
		} );

		$this->term_id = $this->factory->term->create();
		$this->post_id = $this->factory->post->create();

		$_GET = [];
		$_GET['post'] = $this->post_id;

		update_post_meta( $this->post_id, papi_get_page_type_key(), 'properties-page-type' );
		update_term_meta( $this->term_id, papi_get_page_type_key(), 'properties-taxonomy-type' );

		$this->page_type       = papi_get_entry_type_by_id( 'properties-page-type' );
		$this->extra_page_type = papi_get_entry_type_by_id( 'extra-page-type' );
		$this->taxonomy_type   = papi_get_entry_type_by_id( 'properties-taxonomy-type' );
	}

	public function tearDown() {
		parent::tearDown();
		unset(
			$_GET,
			$_POST,
			$this->handler,
			$this->post_id,
			$this->term_id,
			$this->page_type,
			$this->extra_page_type,
			$this->taxonomy_type
		);
	}

	public function test_actions() {
		$this->assertSame( 1, has_action( 'save_post', [$this->handler, 'save_meta_boxes'] ) );
		$this->assertSame( 1, has_action( 'created_term', [$this->handler, 'save_meta_boxes'] ) );
		$this->assertSame( 1, has_action( 'edit_term', [$this->handler, 'save_meta_boxes'] ) );
	}

	public function test_save_meta_boxes() {
		$property = $this->page_type->get_property( 'string_test' );

		$_POST = papi_test_create_property_post_data( [
			'slug'  => $property->slug,
			'type'  => $property,
			'value' => 'Hello, world!'
		], $_POST );

		$user_id = $this->factory->user->create( [ 'role' => 'administrator' ] );
		wp_set_current_user( $user_id );

		$_POST['papi_meta_nonce'] = wp_create_nonce( 'papi_save_data' );
		$_POST['post_ID'] = $this->post_id;

		$this->handler->save_meta_boxes( $this->post_id, get_post( $this->post_id ) );
		wp_set_current_user( 0 );

		$this->assertSame( 'Hello, world!', papi_get_field( $this->post_id, $property->slug ) );
	}

	public function test_save_meta_boxes_2() {
		$property = $this->page_type->get_property( 'bool_test' );

		$_POST = papi_test_create_property_post_data( [
			'slug'  => $property->slug,
			'type'  => $property,
			'value' => 'on'
		], $_POST );

		$user_id = $this->factory->user->create( [ 'role' => 'administrator' ] );
		wp_set_current_user( $user_id );

		$_POST['papi_meta_nonce'] = wp_create_nonce( 'papi_save_data' );
		$_POST['post_ID'] = $this->post_id;

		$this->handler->save_meta_boxes( $this->post_id, get_post( $this->post_id ) );
		wp_set_current_user( 0 );

		$this->assertTrue( papi_get_field( $this->post_id, $property->slug ) );
	}

	public function test_save_meta_boxes_3() {
		$post_id  = $this->factory->post->create();
		$revs_id  = wp_save_post_revision( $post_id );
		$property = $this->page_type->get_property( 'string_test' );

		$_POST = papi_test_create_property_post_data( [
			'slug'  => $property->slug,
			'type'  => $property,
			'value' => 'Hello, world!'
		], $_POST );

		$user_id = $this->factory->user->create( [ 'role' => 'administrator' ] );
		wp_set_current_user( $user_id );

		$_POST['papi_meta_nonce'] = wp_create_nonce( 'papi_save_data' );
		$_POST['post_ID'] = $revs_id;

		$this->handler->save_meta_boxes( $revs_id, get_post( $revs_id ) );
		wp_set_current_user( 0 );

		$this->assertSame( 'Hello, world!', get_post_meta( $revs_id, unpapify( $property->slug ), true ) );
	}

	/**
	 * Autosave.
	 */
	public function test_save_meta_boxes_4() {
		$post_id  = $this->factory->post->create();
		$property = $this->page_type->get_property( 'string_test' );
		$revs_id  = wp_save_post_revision( $post_id );
		$_POST = papi_test_create_property_post_data( [
			'slug'  => $property->slug,
			'type'  => $property,
			'value' => 'Hello, world!'
		], $_POST );

		$user_id = $this->factory->user->create( [ 'role' => 'administrator' ] );
		wp_set_current_user( $user_id );

		$_POST['papi_meta_nonce'] = wp_create_nonce( 'papi_save_data' );
		$_POST['data'] = ['wp_autosave' => ['post_id' => $revs_id]];

		$this->handler->save_meta_boxes( $post_id, get_post( $post_id ) );
		wp_set_current_user( 0 );

		$this->assertSame( 'Hello, world!', get_post_meta( $post_id, unpapify( $property->slug ), true ) );
	}

	/**
	 * Autosave draft.
	 */
	public function test_save_meta_boxes_5() {
		$post_id  = $this->factory->post->create();
		$property = $this->page_type->get_property( 'string_test' );
		$revs_id  = wp_save_post_revision( $post_id );
		$_POST = papi_test_create_property_post_data( [
			'slug'  => $property->slug,
			'type'  => $property,
			'value' => 'Hello, world!'
		], $_POST );

		$user_id = $this->factory->user->create( [ 'role' => 'administrator' ] );
		wp_set_current_user( $user_id );

		$_POST['papi_meta_nonce'] = wp_create_nonce( 'papi_save_data' );
		$_POST['data'] = ['wp_autosave' => ['post_id' => $revs_id, 'autodraft' => '1']];

		$this->handler->save_meta_boxes( $post_id, get_post( $post_id ) );
		wp_set_current_user( 0 );

		$this->assertSame( 'Hello, world!', get_post_meta( $post_id, unpapify( $property->slug ), true ) );
	}

	/**
	 * @issue 126
	 */
	public function test_save_meta_boxes_apostrophe() {
		$property = $this->page_type->get_property( 'string_test' );

		$_POST = papi_test_create_property_post_data( [
			'slug'  => $property->slug,
			'type'  => $property,
			'value' => "I'm home"
		], $_POST );

		$user_id = $this->factory->user->create( [ 'role' => 'administrator' ] );
		wp_set_current_user( $user_id );

		$_POST['papi_meta_nonce'] = wp_create_nonce( 'papi_save_data' );
		$_POST['post_ID'] = $this->post_id;

		$this->handler->save_meta_boxes( $this->post_id, get_post( $this->post_id ) );
		wp_set_current_user( 0 );

		$this->assertSame( "I'm home", papi_get_field( $this->post_id, $property->slug ) );
	}

{'fraction_non_alphanumeric': 0.18928668049418343, 'fraction_numerical': 0.0029759220849490484, 'mean_word_length': 4.678443420378904, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 257, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1886427', 'n_tokens_mistral': 4685, 'n_tokens_neox': 4349, 'n_words': 640}