Completed
Push — fix/licenses-not-attached-on-o... ( f35ec5...39388a )
by Atanas
315:11 queued 305:16
created

Test_Jetpack_JITM::test_jitm_disabled_by_default()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 8
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
		Functions\when( 'current_user_can' )->justReturn( true );
28
	}
29
30
	/**
31
	 * Tear down.
32
	 *
33
	 * @after
34
	 */
35
	public function tear_down() {
36
		Monkey\tearDown();
37
	}
38
39
	public function test_jitm_disabled_by_default() {
40
		Functions\expect( 'apply_filters' )
41
			->once()
42
			->with(	'jetpack_just_in_time_msgs', false );
43
44
		$jitm = new JITM();
45
		$this->assertFalse( $jitm->register() );
46
	}
47
48
	public function test_jitm_enabled_by_filter() {
49
		Functions\expect( 'apply_filters' )
50
			->once()
51
			->with( 'jetpack_just_in_time_msgs', false )
52
			->andReturn( true );
53
54
		$jitm = new JITM();
55
		$this->assertTrue( $jitm->register() );
56
	}
57
58
	/**
59
	 * This is an example of a test which uses Mockery to tests a class static method.
60
	 *
61
	 * It requires the runInSeparateProcess tag so that the class isn't already autoloaded.
62
	 *
63
	 * @runInSeparateProcess
64
	 */
65
	public function test_prepare_jitms_enqueues_assets() {
66
		$mockAssets = \Mockery::mock( 'alias:Automattic\Jetpack\Assets' );
67
68
		// mock the static method and return a dummy value
69
		$mockAssets
70
			->shouldReceive( 'get_file_url_for_environment' )
71
			->andReturn( 'the_file_url' );
72
73
		$jitm = new JITM();
74
		$screen = (object) array( 'id' => 'jetpack_foo' ); // fake screen object
75
		$jitm->prepare_jitms( $screen );
76
77
		// Assert the action was added
78
		$this->assertNotFalse( has_action( 'admin_enqueue_scripts', array( $jitm, 'jitm_enqueue_files' ) ) );
79
80
		// Set up mocks for a bunch of methods called by the hook.
81
		Functions\expect( 'plugins_url' )->once()->andReturn( 'the_plugin_url' );
82
		Functions\when( 'esc_url_raw' )->justReturn( '' );
83
		Functions\when( 'esc_html__' )->justReturn( '' );
84
		Functions\when( 'wp_create_nonce' )->justReturn( '' );
85
		Functions\when( 'rest_url' )->justReturn( '' );
86
		Functions\expect( 'wp_register_style' )->once()->with(
87
			'jetpack-jitm-css',
88
			'the_plugin_url',
89
			false,
90
			\Mockery::type( 'string' )
91
		);
92
		Functions\expect( 'wp_style_add_data' )->with(
93
			'jetpack-jitm-css',
94
			\Mockery::type( 'string' ),
95
			\Mockery::type( 'string' )
96
		);
97
		Functions\expect( 'wp_enqueue_style' )->once()->with( 'jetpack-jitm-css' );
98
		Functions\expect( 'wp_enqueue_script' )->once()->with(
99
			'jetpack-jitm-new',
100
			'the_file_url',
101
			array( 'jquery' ),
102
			JITM::PACKAGE_VERSION,
103
			true
104
		);
105
		Functions\expect( 'wp_localize_script' )->once()->with(
106
			'jetpack-jitm-new',
107
			'jitm_config',
108
			\Mockery::type( 'array' )
109
		);
110
111
		// Do the action that we asserted was added.
112
		$jitm->jitm_enqueue_files();
113
	}
114
115
}
116