<commit_before><?php
/**
 * TrackingTest
 *
 * @package   Google\Site_Kit\Tests\Core\Util
 * @copyright 2019 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\Core\Util;

use Google\Site_Kit\Context;
use Google\Site_Kit\Core\Storage\User_Options;
use Google\Site_Kit\Core\Util\Tracking;
use Google\Site_Kit\Tests\TestCase;

/**
 * @group Util
 */
class TrackingTest extends TestCase {

	public function setUp() {
		parent::setUp();
		// Unregister all registered user meta.
		global $wp_meta_keys;
		unset( $wp_meta_keys['user'] );
	}

	public function test_register() {
		remove_all_filters( 'googlesitekit_inline_base_data' );
		$user_options = new User_Options( new Context( GOOGLESITEKIT_PLUGIN_MAIN_FILE ) );
		$tracking     = new Tracking( $user_options );
		$this->assertArrayNotHasKey( $user_options->get_meta_key( Tracking::OPTION ), get_registered_meta_keys( 'user' ) );

		$tracking->register();

		$this->assertArrayHasKey( $user_options->get_meta_key( Tracking::OPTION ), get_registered_meta_keys( 'user' ) );
		$this->assertTrue( has_filter( 'googlesitekit_inline_base_data' ) );
	}

	public function test_is_active() {
		$user_id = $this->factory()->user->create( array( 'role' => 'administrator' ) );
		wp_set_current_user( $user_id );
		$tracking = new Tracking( new User_Options( new Context( GOOGLESITEKIT_PLUGIN_MAIN_FILE ) ) );
		$this->opt_out_from_tracking();

		$this->assertFalse( $tracking->is_active() );

		$this->opt_in_to_tracking();

		$this->assertTrue( $tracking->is_active() );
	}

	protected function opt_in_to_tracking( $network_wide = false ) {
		update_user_option( get_current_user_id(), Tracking::OPTION, 1, $network_wide );
	}

	protected function opt_out_from_tracking( $network_wide = false ) {
		update_user_option( get_current_user_id(), Tracking::OPTION, 0, $network_wide );
	}
}<commit_msg>Update TrackingTest.<commit_after><?php
/**
 * TrackingTest
 *
 * @package   Google\Site_Kit\Tests\Core\Util
 * @copyright 2019 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\Core\Util;

use Google\Site_Kit\Core\Util\Tracking;
use Google\Site_Kit\Core\Util\Tracking_Consent;
use Google\Site_Kit\Tests\TestCase;

/**
 * @group Util
 */
class TrackingTest extends TestCase {

	public function test_register() {
		$tracking_consent_mock = $this->getTrackingConsentMock( array( 'register', 'get' ) );
		$tracking_consent_mock->expects( $this->once() )->method( 'register' );
		$tracking = new Tracking( $tracking_consent_mock );
		remove_all_filters( 'googlesitekit_inline_base_data' );

		$tracking->register();

		$this->assertTrue( has_filter( 'googlesitekit_inline_base_data' ) );
		$base_data = apply_filters( 'googlesitekit_inline_base_data', array() );
		$this->assertArrayHasKey( 'trackingEnabled', $base_data );
		$this->assertEquals( Tracking::TRACKING_ID, $base_data['trackingID'] );
	}

	public function test_is_active() {
		$tracking_consent_mock = $this->getTrackingConsentMock( 'get' );
		$tracking              = new Tracking( $tracking_consent_mock );

		// Set Tracking_Consent::get() to return an empty string on the first call, and '1' on the second.
		$tracking_consent_mock->expects( $this->any() )->method( 'get' )->willReturnOnConsecutiveCalls( '', '1' );

		$this->assertFalse( $tracking->is_active() );

		// User option change is simulated with mock above.

		$this->assertTrue( $tracking->is_active() );
	}

	protected function getTrackingConsentMock( $methods ) {
		return $this->getMockBuilder( Tracking_Consent::class )
					->disableOriginalConstructor()
					->setMethods( (array) $methods )
					->getMock();
	}
}
{'fraction_non_alphanumeric': 0.14308134972534658, 'fraction_numerical': 0.0052314935914203504, 'mean_word_length': 4.993730407523511, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 40, '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': '4503422', 'n_tokens_mistral': 1382, 'n_tokens_neox': 1282, 'n_words': 235}