<commit_before><?php
/**
 * \Google\Site_Kit\Tests\Core\Util\Migration_1_8_1Test
 *
 * @package   Google\Site_Kit\Tests\Core\Util
 * @copyright 2020 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\Authentication\Authentication;
use Google\Site_Kit\Core\Authentication\Google_Proxy;
use Google\Site_Kit\Core\Authentication\Verification_File;
use Google\Site_Kit\Core\Authentication\Verification_Meta;
use Google\Site_Kit\Core\Authentication\Clients\OAuth_Client;
use Google\Site_Kit\Core\Storage\Options;
use Google\Site_Kit\Core\Storage\User_Options;
use Google\Site_Kit\Core\Util\Migration_1_8_1;
use Google\Site_Kit\Tests\TestCase;

class Migration_1_8_1Test extends TestCase {

	const FILE_VERIFICATION = Verification_File::OPTION;
	const META_VERIFICATION = Verification_Meta::OPTION;

	/**
	 * @var Context
	 */
	protected $context;

	/**
	 * @var Options
	 */
	protected $options;

	/**
	 * @var User_Options
	 */
	protected $user_options;

	/**
	 * @var Authentication
	 */
	protected $authentication;

	/**
	 * @var Migration_1_8_1
	 */
	protected $migration;

	private $temp_received_identifiers;

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

		$this->context        = new Context( GOOGLESITEKIT_PLUGIN_MAIN_FILE );
		$this->options        = new Options( $this->context );
		$this->user_options   = new User_Options( $this->context );
		$this->authentication = new Authentication( $this->context, $this->options, $this->user_options );
		$this->migration      = new Migration_1_8_1( $this->context, $this->options, $this->user_options, $this->authentication );

		$this->temp_received_identifiers = null;
	}

	public function test_register() {
		remove_all_actions( 'admin_init' );
		$this->migration->register();

		$this->assertTrue( has_action( 'admin_init' ) );
	}

	public function test_migrate_without_using_proxy() {
		$this->fake_site_connection();
		$result = $this->migration->migrate();
		$this->assertFalse( $result );
		$this->assertNotEquals( Migration_1_8_1::DB_VERSION, $this->get_db_version() );
	}

	public function test_migrate_without_credentials() {
		$result = $this->migration->migrate();
		$this->assertFalse( $result );
		$this->assertNotEquals( Migration_1_8_1::DB_VERSION, $this->get_db_version() );
	}

	public function test_migrate_without_users() {
		$this->fake_proxy_site_connection();

		$result = $this->migration->migrate();
		$this->assertTrue( $result );
		$this->assertEquals( Migration_1_8_1::DB_VERSION, $this->get_db_version() );
	}

	public function test_migrate_without_problem_cases() {
		$this->create_user( 'administrator', self::FILE_VERIFICATION );
		$this->create_user( 'subscriber', false );

		$this->fake_proxy_site_connection();

		$result = $this->migration->migrate();
		$this->assertTrue( $result );
		$this->assertEquals( Migration_1_8_1::DB_VERSION, $this->get_db_version() );
	}

	public function test_migrate_with_problem_cases() {
		$this->create_user( 'administrator', self::FILE_VERIFICATION );
		$this->create_user( 'subscriber', false );
		$this->create_user( 'subscriber', false );
		$this->create_user( 'administrator', false );

		$problem_cases = array(
			$this->create_user( 'subscriber', self::META_VERIFICATION ),
			$this->create_user( 'editor', self::FILE_VERIFICATION ),
		);

		$this->fake_proxy_site_connection();
		$this->stub_mark_notifications_response();

		$result = $this->migration->migrate();
		$this->assertTrue( $result );
		$this->assertReceivedIdentifiers(
			array_map(
				function( $user ) {
					return $user->user_email;
				},
				$problem_cases
			)
		);
		foreach ( $problem_cases as $user ) {
			$this->assertNoSiteKitUserOptions( $user->ID );<commit_msg>Add coverage for Google profile email support to 1.8.1 migration routine.<commit_after><?php
/**
 * \Google\Site_Kit\Tests\Core\Util\Migration_1_8_1Test
 *
 * @package   Google\Site_Kit\Tests\Core\Util
 * @copyright 2020 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\Authentication\Authentication;
use Google\Site_Kit\Core\Authentication\Google_Proxy;
use Google\Site_Kit\Core\Authentication\Profile;
use Google\Site_Kit\Core\Authentication\Verification_File;
use Google\Site_Kit\Core\Authentication\Verification_Meta;
use Google\Site_Kit\Core\Authentication\Clients\OAuth_Client;
use Google\Site_Kit\Core\Storage\Options;
use Google\Site_Kit\Core\Storage\User_Options;
use Google\Site_Kit\Core\Util\Migration_1_8_1;
use Google\Site_Kit\Tests\TestCase;

class Migration_1_8_1Test extends TestCase {

	const FILE_VERIFICATION = Verification_File::OPTION;
	const META_VERIFICATION = Verification_Meta::OPTION;

	/**
	 * @var Context
	 */
	protected $context;

	/**
	 * @var Options
	 */
	protected $options;

	/**
	 * @var User_Options
	 */
	protected $user_options;

	/**
	 * @var Authentication
	 */
	protected $authentication;

	/**
	 * @var Migration_1_8_1
	 */
	protected $migration;

	private $temp_received_identifiers;

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

		$this->context        = new Context( GOOGLESITEKIT_PLUGIN_MAIN_FILE );
		$this->options        = new Options( $this->context );
		$this->user_options   = new User_Options( $this->context );
		$this->authentication = new Authentication( $this->context, $this->options, $this->user_options );
		$this->migration      = new Migration_1_8_1( $this->context, $this->options, $this->user_options, $this->authentication );

		$this->temp_received_identifiers = null;
	}

	public function test_register() {
		remove_all_actions( 'admin_init' );
		$this->migration->register();

		$this->assertTrue( has_action( 'admin_init' ) );
	}

	public function test_migrate_without_using_proxy() {
		$this->fake_site_connection();
		$result = $this->migration->migrate();
		$this->assertFalse( $result );
		$this->assertNotEquals( Migration_1_8_1::DB_VERSION, $this->get_db_version() );
	}

	public function test_migrate_without_credentials() {
		$result = $this->migration->migrate();
		$this->assertFalse( $result );
		$this->assertNotEquals( Migration_1_8_1::DB_VERSION, $this->get_db_version() );
	}

	public function test_migrate_without_users() {
		$this->fake_proxy_site_connection();

		$result = $this->migration->migrate();
		$this->assertTrue( $result );
		$this->assertEquals( Migration_1_8_1::DB_VERSION, $this->get_db_version() );
	}

	public function test_migrate_without_problem_cases() {
		$this->create_user( 'administrator', self::FILE_VERIFICATION );
		$this->create_user( 'subscriber', false );

		$this->fake_proxy_site_connection();

		$result = $this->migration->migrate();
		$this->assertTrue( $result );
		$this->assertEquals( Migration_1_8_1::DB_VERSION, $this->get_db_version() );
	}

	public function test_migrate_with_problem_cases() {
		$this->create_user( 'administrator', self::FILE_VERIFICATION );
		$this->create_user( 'subscriber', false );
		$this->create_user( 'subscriber', false );
		$this->create_user( 'administrator', false );

		$problem_cases = array(
			$this->create_user( 'subscriber', self::META_VERIFICATION ),
			$this->create_user( 'editor', self::FILE_VERIFICATION ),
		);

		// Set Google profile data for one of the problematic users (see below).
		$profile = new Profile( new User_Options( $this->context, $problem_cases[0]->ID ) );
		$profile->set(
			array(
				'email' => '<EMAIL>',
				'photo' => 'https://wapu.us/wp-content/uploads/2017/11/WapuuFinal-100x138.png',
			)
		);

		// Ensure the functionality uses Google profile email preferably, but
		// falls back to WordPress user email if Google profile data not
		// present.
		$expected_identifiers = array(
			'<EMAIL>',
			$problem_cases[1]->user_email,
		);

		$this->fake_proxy_site_connection();
		$this->stub_mark_notifications_response();

		$result = $this->migration->migrate();
		$this->assertTrue( $result );
		$this->assertReceivedIdentifiers( $expected_identifiers );
		foreach ( $problem_cases as $user ) {
			$this->assertNoSiteKitUserOptions( $user->ID );
{'fraction_non_alphanumeric': 0.14689197344598673, 'fraction_numerical': 0.010500905250452625, 'mean_word_length': 4.939784946236559, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 126, 'https://': 5, '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': '1887818', 'n_tokens_mistral': 2965, 'n_tokens_neox': 2757, 'n_words': 457}