<commit_before><?php

use WP_CLI\WpOrgApi;

require_once dirname( __DIR__ ) . '/mock-requests-transport.php';

class WpOrgApiTest extends PHPUnit_Framework_TestCase {

	public function data_http_request_verify() {
		return [
			'can retrieve core checksums' => [
				'get_core_checksums',
				[ 'version' => 'trunk' ],
				[],
				'https://api.wordpress.org/core/checksums/1.0/?version=trunk&locale=en_US',
				[],
			],
			'can retrieve core checksums for a specific locale' => [
				'get_core_checksums',
				[
					'version' => '4.5',
					'locale'  => 'de_DE',
				],
				[],
				'https://api.wordpress.org/core/checksums/1.0/?version=4.5&locale=de_DE',
				[],
			],
			'can retrieve plugin checksums' => [
				'get_plugin_checksums',
				[
					'plugin'  => 'hello-dolly',
					'version' => '1.0',
				],
				[],
				'https://downloads.wordpress.org/plugin-checksums/hello-dolly/1.0.json',
				[],
			],
			'can retrieve a download offer for core' => [
				'get_download_offer',
				[],
				[],
				'https://api.wordpress.org/core/version-check/1.7/?locale=en_US',
				[],
			],
			'can retrieve a download offer for core for a specific locale' => [
				'get_download_offer',
				[ 'locale' => 'de_DE' ],
				[],
				'https://api.wordpress.org/core/version-check/1.7/?locale=de_DE',
				[],
			],
			'can retrieve salts' => [
				'get_salts',
				[],
				[],
				'https://api.wordpress.org/secret-key/1.1/salt/',
				[],
			],
			'defaults to secure requests' => [
				'get_salts',
				[],
				[],
				'https://api.wordpress.org/secret-key/1.1/salt/',
				[ 'verify' => true ],
			],
			'can explicitly request secure requests' => [
				'get_salts',
				[],
				[ 'insecure' => false ],
				'https://api.wordpress.org/secret-key/1.1/salt/',
				[
					'insecure' => false,
					'verify'   => true,
				],
			],
			'can explicitly request insecure requests' => [
				'get_salts',
				[],
				[ 'insecure' => true ],
				'https://api.wordpress.org/secret-key/1.1/salt/',
				[
					'insecure' => true,
					'verify'   => false,
				],
			],
		];
	}

	/**
	 * @dataProvider data_http_request_verify()
	 */
	public function test_http_request_verify( $method, $arguments, $options, $expected_url, $expected_options )
	{
		if ( isset( $options['insecure'] ) && $options['insecure'] === true ) {
			// Create temporary file to use as a bad certificate file.
			$bad_cacert_path = tempnam( sys_get_temp_dir(), 'wp-cli-badcacert-pem-' );
			file_put_contents(
				$bad_cacert_path,
				"-----BEGIN CERTIFICATE-----\nasdfasdf\n-----END CERTIFICATE-----\n"
			);

			$options = array_merge( [ 'verify' => $bad_cacert_path ], $options );
		}

		$transport_spy                 = new Mock_Requests_Transport();
		$options['transport']          = $transport_spy;
		$expected_options['transport'] = $transport_spy;

		$wp_org_api = new WpOrgApi( $options );
		try {
			$wp_org_api->$method( ...$arguments );
		} catch ( RuntimeException $exception ) {
		}

		// Undo bad CAcert hack before asserting.
		if ( isset( $bad_cacert_path ) ) {
			unlink( $bad_cacert_path );
		}

		$this->assertCount( 1, $transport_spy->requests );
		$this->assertEquals( $expected_url, $transport_spy->requests[0]['url'] );
		foreach ( $expected_options as $key => $value ) {
			$this->assertEquals( $value, $transport_spy->requests[0]['options'][ $key ] );
		}
	}
}<commit_msg>Add tests<commit_after><?php

use WP_CLI\WpOrgApi;

require_once dirname( __DIR__ ) . '/mock-requests-transport.php';

class WpOrgApiTest extends PHPUnit_Framework_TestCase {

	public function data_http_request_verify() {
		return [
			'can retrieve core checksums'              => [
				'get_core_checksums',
				[ 'version' => 'trunk' ],
				[],
				'https://api.wordpress.org/core/checksums/1.0/?version=trunk&locale=en_US',
				[],
			],
			'can retrieve core checksums for a specific locale' => [
				'get_core_checksums',
				[
					'version' => '4.5',
					'locale'  => 'de_DE',
				],
				[],
				'https://api.wordpress.org/core/checksums/1.0/?version=4.5&locale=de_DE',
				[],
			],
			'can retrieve plugin checksums'            => [
				'get_plugin_checksums',
				[
					'plugin'  => 'hello-dolly',
					'version' => '1.0',
				],
				[],
				'https://downloads.wordpress.org/plugin-checksums/hello-dolly/1.0.json',
				[],
			],
			'can retrieve a download offer for core'   => [
				'get_download_offer',
				[],
				[],
				'https://api.wordpress.org/core/version-check/1.7/?locale=en_US',
				[],
			],
			'can retrieve a download offer for core for a specific locale' => [
				'get_download_offer',
				[ 'locale' => 'de_DE' ],
				[],
				'https://api.wordpress.org/core/version-check/1.7/?locale=de_DE',
				[],
			],
			'can retrieve salts'                       => [
				'get_salts',
				[],
				[],
				'https://api.wordpress.org/secret-key/1.1/salt/',
				[],
			],
			'defaults to secure requests'              => [
				'get_salts',
				[],
				[],
				'https://api.wordpress.org/secret-key/1.1/salt/',
				[ 'verify' => true ],
			],
			'can explicitly request secure requests'   => [
				'get_salts',
				[],
				[ 'insecure' => false ],
				'https://api.wordpress.org/secret-key/1.1/salt/',
				[
					'insecure' => false,
					'verify'   => true,
				],
			],
			'can explicitly request insecure requests' => [
				'get_salts',
				[],
				[ 'insecure' => true ],
				'https://api.wordpress.org/secret-key/1.1/salt/',
				[
					'insecure' => true,
					'verify'   => false,
				],
			],
		];
	}

	/**
	 * @dataProvider data_http_request_verify()
	 */
	public function test_http_request_verify( $method, $arguments, $options, $expected_url, $expected_options ) {
		if ( isset( $options['insecure'] ) && true === $options['insecure'] ) {
			// Create temporary file to use as a bad certificate file.
			$bad_cacert_path = tempnam( sys_get_temp_dir(), 'wp-cli-badcacert-pem-' );
			file_put_contents(
				$bad_cacert_path,
				"-----BEGIN CERTIFICATE-----\nasdfasdf\n-----END CERTIFICATE-----\n"
			);

			$options = array_merge( [ 'verify' => $bad_cacert_path ], $options );
		}

		$transport_spy                 = new Mock_Requests_Transport();
		$options['transport']          = $transport_spy;
		$expected_options['transport'] = $transport_spy;

		$wp_org_api = new WpOrgApi( $options );
		try {
			$wp_org_api->$method( ...$arguments );
		} catch ( RuntimeException $exception ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
		}

		// Undo bad CAcert hack before asserting.
		if ( isset( $bad_cacert_path ) ) {
			unlink( $bad_cacert_path );
		}

		$this->assertCount( 1, $transport_spy->requests );
		$this->assertEquals( $expected_url, $transport_spy->requests[0]['url'] );
		foreach ( $expected_options as $key => $value ) {
			$this->assertEquals( $value, $transport_spy->requests[0]['options'][ $key ] );
		}
	}
}
{'fraction_non_alphanumeric': 0.18539161044294514, 'fraction_numerical': 0.00792021120563215, 'mean_word_length': 2.7303063457330414, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 65, 'https://': 18, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3371495', 'n_tokens_mistral': 3012, 'n_tokens_neox': 2456, 'n_words': 387}