Code Duplication    Length = 23-23 lines in 2 locations

projects/packages/connection/tests/php/test-rest-endpoints.php 2 locations

@@ 339-361 (lines=23) @@
336
	/**
337
	 * Testing the `connection/register` endpoint.
338
	 */
339
	public function test_connection_register() {
340
		add_filter( 'pre_http_request', array( static::class, 'intercept_register_request' ), 10, 3 );
341
342
		$this->request = new WP_REST_Request( 'POST', '/jetpack/v4/connection/register' );
343
		$this->request->set_header( 'Content-Type', 'application/json' );
344
345
		$this->request->set_body( wp_json_encode( array( 'registration_nonce' => wp_create_nonce( 'jetpack-registration-nonce' ) ) ) );
346
347
		$response = $this->server->dispatch( $this->request );
348
		$data     = $response->get_data();
349
350
		remove_filter( 'pre_http_request', array( static::class, 'intercept_register_request' ), 10 );
351
352
		// Manually clears filter added by Manager::register().
353
		remove_filter( 'jetpack_use_iframe_authorization_flow', '__return_false', 20 );
354
355
		$this->assertEquals( 200, $response->get_status() );
356
		$this->assertSame( 0, strpos( $data['authorizeUrl'], 'https://jetpack.wordpress.com/jetpack.authorize/' ) );
357
358
		// Asserts jetpack_register_site_rest_response filter is being properly hooked to add data from wpcom register endpoint response.
359
		$this->assertFalse( $data['allowInplaceAuthorization'] );
360
		$this->assertSame( '', $data['alternateAuthorizeUrl'] );
361
	}
362
363
	/**
364
	 * Testing the `connection/register` endpoint with allow_inplace_authorization as true.
@@ 390-412 (lines=23) @@
387
	/**
388
	 * Testing the `connection/register` endpoint with alternate_authorization_url
389
	 */
390
	public function test_connection_register_with_alternate_auth_url() {
391
		add_filter( 'pre_http_request', array( static::class, 'intercept_register_request_with_alternate_auth_url' ), 10, 3 );
392
393
		$this->request = new WP_REST_Request( 'POST', '/jetpack/v4/connection/register' );
394
		$this->request->set_header( 'Content-Type', 'application/json' );
395
396
		$this->request->set_body( wp_json_encode( array( 'registration_nonce' => wp_create_nonce( 'jetpack-registration-nonce' ) ) ) );
397
398
		$response = $this->server->dispatch( $this->request );
399
		$data     = $response->get_data();
400
401
		remove_filter( 'pre_http_request', array( static::class, 'intercept_register_request_with_alternate_auth_url' ), 10 );
402
403
		// Manually clears filter added by Manager::register().
404
		remove_filter( 'jetpack_use_iframe_authorization_flow', '__return_false', 20 );
405
406
		$this->assertEquals( 200, $response->get_status() );
407
		$this->assertSame( 0, strpos( $data['authorizeUrl'], 'https://jetpack.wordpress.com/jetpack.authorize/' ) );
408
409
		// Asserts jetpack_register_site_rest_response filter is being properly hooked to add data from wpcom register endpoint response.
410
		$this->assertFalse( $data['allowInplaceAuthorization'] );
411
		$this->assertSame( 'https://dummy.com', $data['alternateAuthorizeUrl'] );
412
	}
413
414
	/**
415
	 * This filter callback allow us to skip the database query by `Jetpack_Options` to retrieve the option.