<commit_before><?php
/**
 * Class Google\Site_Kit\Tests\Modules\Thank_With_Google\Web_TagTest
 *
 * @package   Google\Site_Kit\Tests\Modules\Thank_With_Google
 * @copyright 2022 Google LLC
 * @license   https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
 * @link      https://sitekit.withgoogle.com
 */

namespace Google\Site_Kit\Tests\Modules\Thank_With_Google;

use Google\Site_Kit\Modules\Thank_With_Google;
use Google\Site_Kit\Modules\Thank_With_Google\Web_Tag;
use Google\Site_Kit\Tests\TestCase;

/**
 * @group Modules
 * @group Thank_With_Google
 */
class Web_TagTest extends TestCase {
	/**
	 * Web_Tag instance.
	 *
	 * @var Web_Tag
	 */
	private $web_tag;

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

		$this->web_tag = new Web_Tag( '12345', Thank_With_Google::MODULE_SLUG );

		$this->web_tag->set_publication_id( '12345' );
		$this->web_tag->set_button_post_types( array( 'post' ) );
	}

	public function test_content_placeholder_not_inserted_on_unselected_button_post_types() {
		remove_all_filters( 'the_content' );

		$this->web_tag->set_button_placement( 'static_auto' );
		$this->web_tag->register();

		$post_ID = $this->factory()->post->create(
			array(
				'post_type'    => 'page',
				'post_content' => '<p>Hello World</p><p>Goodbye World</p>',
			)
		);
		$this->go_to( get_permalink( $post_ID ) );

		$output = get_echo( 'the_content' );

		$this->assertStringNotContainsString( 'Thank with Google snippet added by Site Kit', $output );
		$this->assertStringNotContainsString( '<button twg-button', $output );
	}

	public function test_content_placeholder_inserted_on_button_post_types() {
		remove_all_filters( 'the_content' );

		$this->web_tag->set_button_placement( 'static_auto' );
		$this->web_tag->register();

		$this->create_post_and_go_to_it();

		$output = get_echo( 'the_content' );

		$this->assertStringContainsString( 'Thank with Google snippet added by Site Kit', $output );
		$this->assertStringContainsString( '<button twg-button', $output );
	}

	public function test_content_placeholder_not_inserted_on_dynamic_button_placement() {
		remove_all_filters( 'the_content' );

		$this->web_tag->set_button_placement( 'dynamic_low' );
		$this->web_tag->register();

		$this->create_post_and_go_to_it();

		$output = get_echo( 'the_content' );

		$this->assertStringNotContainsString( 'Thank with Google snippet added by Site Kit', $output );
		$this->assertStringNotContainsString( '<button twg-button', $output );
	}

	public function test_content_placeholder_inserted_static_below_content() {
		remove_all_filters( 'the_content' );

		$this->web_tag->set_button_placement( 'static_below-content' );
		$this->web_tag->register();

		$this->create_post_and_go_to_it();

		$output  = get_echo( 'the_content' );
		$content = get_the_content();

		$this->assertStringStartsWith( $content, $output );
		$this->assertStringContainsString( 'Thank with Google snippet added by Site Kit', $output );
		$this->assertStringContainsString( '<button twg-button', $output );
	}

	public function test_content_placeholder_inserted_static_above_content() {
		remove_all_filters( 'the_content' );

		$this->web_tag->set_button_placement( 'static_above-content' );
		$this->web_tag->register();

		$this->create_post_and_go_to_it();

		$output  = get_echo( 'the_content' );
		$content = get_the_content();

		$this->assertStringEndsWith( $content, $output );
		$this->assertStringContainsString( 'Thank with Google snippet added by Site Kit', $output );
		$this->assertStringContainsString( '<button twg-button', $output );
	}

	public function test_content_placeholder_inserted_static_below_first_paragraph() {
		remove_all_filters( 'the_content' );

		$this->web_tag->set_button_placement( 'static_below-first-paragraph' );
		$this->web_tag->register();

		$this->create_post_and_go_to_it();

		$output = get_echo( 'the_content' );

		$this->assertStringStartsWith( '<p>Hello World</p>', $output );
		$this->assertStringEndsWith( '<p>Goodbye World</p>', $output );
		$this->assertStringContainsString( 'Thank with Google snippet added by Site Kit', $output );
		$this->assertStringContainsString( '<button twg-button', $output );
	}

	private function create_post_and_go_to_it() {
		$post_ID = $this->factory()->post->create(
			array(
				'post_content' => '<p>Hello World</p><p>Goodbye World</p>',
				'post_status'  => 'publish',
			)
		);
		$this->go_to( get_permalink( $post_ID ) );
	}
}<commit_msg>Use different approach to test the_content filter as get_the_content() does not work.<commit_after><?php
/**
 * Class Google\Site_Kit\Tests\Modules\Thank_With_Google\Web_TagTest
 *
 * @package   Google\Site_Kit\Tests\Modules\Thank_With_Google
 * @copyright 2022 Google LLC
 * @license   https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
 * @link      https://sitekit.withgoogle.com
 */

namespace Google\Site_Kit\Tests\Modules\Thank_With_Google;

use Google\Site_Kit\Modules\Thank_With_Google;
use Google\Site_Kit\Modules\Thank_With_Google\Web_Tag;
use Google\Site_Kit\Tests\TestCase;

/**
 * @group Modules
 * @group Thank_With_Google
 */
class Web_TagTest extends TestCase {
	/**
	 * Web_Tag instance.
	 *
	 * @var Web_Tag
	 */
	private $web_tag;

	private $dummy_content = '<p>Hello World</p><p>Goodbye World</p>';

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

		$this->web_tag = new Web_Tag( '12345', Thank_With_Google::MODULE_SLUG );

		$this->web_tag->set_publication_id( '12345' );
		$this->web_tag->set_button_post_types( array( 'post' ) );

		remove_all_filters( 'the_content' );
		$this->web_tag->register();
	}

	public function test_content_placeholder_not_inserted_on_unselected_button_post_types() {
		$this->web_tag->set_button_placement( 'static_auto' );

		$post_ID = $this->factory()->post->create( array( 'post_type' => 'page' ) );
		$this->go_to( get_permalink( $post_ID ) );

		$output = apply_filters( 'the_content', $this->dummy_content );

		$this->assertStringNotContainsString( 'Thank with Google snippet added by Site Kit', $output );
		$this->assertStringNotContainsString( '<button twg-button', $output );
	}

	public function test_content_placeholder_inserted_on_button_post_types() {
		$this->web_tag->set_button_placement( 'static_auto' );

		$this->create_post_and_go_to_it();

		$output = apply_filters( 'the_content', $this->dummy_content );

		$this->assertStringContainsString( 'Thank with Google snippet added by Site Kit', $output );
		$this->assertStringContainsString( '<button twg-button', $output );
	}

	public function test_content_placeholder_not_inserted_on_dynamic_button_placement() {
		$this->web_tag->set_button_placement( 'dynamic_low' );

		$this->create_post_and_go_to_it();

		$output = apply_filters( 'the_content', $this->dummy_content );

		$this->assertStringNotContainsString( 'Thank with Google snippet added by Site Kit', $output );
		$this->assertStringNotContainsString( '<button twg-button', $output );
	}

	public function test_content_placeholder_inserted_static_below_content() {
		$this->web_tag->set_button_placement( 'static_below-content' );

		$this->create_post_and_go_to_it();

		$output = apply_filters( 'the_content', $this->dummy_content );

		$this->assertStringStartsWith( $this->dummy_content, $output );
		$this->assertStringContainsString( 'Thank with Google snippet added by Site Kit', $output );
		$this->assertStringContainsString( '<button twg-button', $output );
	}

	public function test_content_placeholder_inserted_static_above_content() {
		$this->web_tag->set_button_placement( 'static_above-content' );

		$this->create_post_and_go_to_it();

		$output = apply_filters( 'the_content', $this->dummy_content );

		$this->assertStringEndsWith( $this->dummy_content, $output );
		$this->assertStringContainsString( 'Thank with Google snippet added by Site Kit', $output );
		$this->assertStringContainsString( '<button twg-button', $output );
	}

	public function test_content_placeholder_inserted_static_below_first_paragraph() {
		$this->web_tag->set_button_placement( 'static_below-first-paragraph' );

		$this->create_post_and_go_to_it();

		$output = apply_filters( 'the_content', $this->dummy_content );

		$this->assertStringStartsWith( '<p>Hello World</p>', $output );
		$this->assertStringEndsWith( '<p>Goodbye World</p>', $output );
		$this->assertStringContainsString( 'Thank with Google snippet added by Site Kit', $output );
		$this->assertStringContainsString( '<button twg-button', $output );
	}

	private function create_post_and_go_to_it() {
		$post_ID = $this->factory()->post->create();
		$this->go_to( get_permalink( $post_ID ) );
	}
}
{'fraction_non_alphanumeric': 0.14443411573320938, 'fraction_numerical': 0.004183128050197537, 'mean_word_length': 5.610599078341014, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 142, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, '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': '1565554', 'n_tokens_mistral': 3069, 'n_tokens_neox': 2853, 'n_words': 496}