Completed
Push — update/phpunit-php-8 ( e34c58...05aa04 )
by
unknown
137:27 queued 129:12
created

test_pre_connection_jitms_disabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php  // phpcs:disable
2
3
namespace Automattic\Jetpack;
4
5
use Automattic\Jetpack\JITMS\JITM;
6
use Automattic\Jetpack\JITMS\Pre_Connection_JITM;
7
use Brain\Monkey;
8
use Brain\Monkey\Actions;
9
use Brain\Monkey\Functions;
10
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
11
use PHPUnit\Framework\TestCase;
12
13
class Test_Jetpack_JITM extends TestCase {
14
	use MockeryPHPUnitIntegration;
15
16
	/**
17
	 * Set up.
18
	 *
19
	 * @before
20
	 */
21
	public function set_up() {
22
		Monkey\setUp();
23
24
		Functions\when( 'get_current_screen' )->justReturn( new \stdClass() );
25
		Functions\when( 'site_url' )->justReturn( 'unit-test' );
26
		Functions\when( 'wp_get_environment_type' )->justReturn( '' );
27
	}
28
29
	/**
30
	 * Tear down.
31
	 *
32
	 * @after
33
	 */
34
	public function tear_down() {
35
		Monkey\tearDown();
36
	}
37
38 View Code Duplication
	public function test_jitm_disabled_by_filter() {
39
		Functions\expect( 'apply_filters' )->once()->with(
40
			'jetpack_just_in_time_msgs',
41
			false
42
		)->andReturn( false );
43
44
		$jitm = new JITM();
45
		$this->assertFalse( $jitm->register() );
46
	}
47
48 View Code Duplication
	public function test_jitm_enabled_by_default() {
49
		Functions\expect( 'apply_filters' )->once()->with(
50
			'jetpack_just_in_time_msgs',
51
			false
52
		)->andReturn( true );
53
54
		$jitm = new JITM();
55
		$this->assertTrue( $jitm->register() );
56
	}
57
58
	/**
59
	 * Pre-connection JITMs are disabled by default,
60
	 * unless a filter is used.
61
	 */
62
	public function test_pre_connection_jitms_disabled() {
63
		add_filter( 'jetpack_pre_connection_prompt_helpers', '__return_false' );
64
65
		$jitm = new Pre_Connection_JITM();
66
		$this->assertEmpty( $jitm->get_messages( '/wp:edit-post:admin_notices/', '', false ) );
67
	}
68
69
	/**
70
	 * This is an example of a test which uses Mockery to tests a class static method.
71
	 *
72
	 * It requires the runInSeparateProcess tag so that the class isn't already autoloaded.
73
	 *
74
	 * @runInSeparateProcess
75
	 */
76
	public function test_prepare_jitms_enqueues_assets() {
77
		$mockAssets = \Mockery::mock( 'alias:Automattic\Jetpack\Assets' );
78
79
		// mock the static method and return a dummy value
80
		$mockAssets
81
			->shouldReceive( 'get_file_url_for_environment' )
82
			->andReturn( 'the_file_url' );
83
84
		$jitm = new JITM();
85
		$screen = (object) array( 'id' => 'jetpack_foo' ); // fake screen object
86
		$jitm->prepare_jitms( $screen );
87
88
		// Assert the action was added
89
		$this->assertNotFalse( has_action( 'admin_enqueue_scripts', array( $jitm, 'jitm_enqueue_files' ) ) );
90
91
		// Set up mocks for a bunch of methods called by the hook.
92
		Functions\expect( 'plugins_url' )->once()->andReturn( 'the_plugin_url' );
93
		Functions\when( 'esc_url_raw' )->justReturn( '' );
94
		Functions\when( 'esc_html__' )->justReturn( '' );
95
		Functions\when( 'wp_create_nonce' )->justReturn( '' );
96
		Functions\when( 'rest_url' )->justReturn( '' );
97
		Functions\expect( 'wp_register_style' )->once()->with(
98
			'jetpack-jitm-css',
99
			'the_plugin_url',
100
			false,
101
			\Mockery::type( 'string' )
102
		);
103
		Functions\expect( 'wp_style_add_data' )->with(
104
			'jetpack-jitm-css',
105
			\Mockery::type( 'string' ),
106
			\Mockery::type( 'string' )
107
		);
108
		Functions\expect( 'wp_enqueue_style' )->once()->with( 'jetpack-jitm-css' );
109
		Functions\expect( 'wp_enqueue_script' )->once()->with(
110
			'jetpack-jitm-new',
111
			'the_file_url',
112
			array( 'jquery' ),
113
			JITM::PACKAGE_VERSION,
114
			true
115
		);
116
		Functions\expect( 'wp_localize_script' )->once()->with(
117
			'jetpack-jitm-new',
118
			'jitm_config',
119
			\Mockery::type( 'array' )
120
		);
121
122
		// Do the action that we asserted was added.
123
		$jitm->jitm_enqueue_files();
124
	}
125
126
}
127