Passed
Push — debug-scrutinizer ( ae1241...11c09a )
by Rahul
14:31
created

Brightcove_Video_Field::test_js_wp_templates()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
1
<?php
2
/**
3
 * Test for Brightcove Video Field register.
4
 *
5
 * @package cmb2-brightcove-video-field
6
 */
7
8
namespace CMB2\Tests\BrightcoveVideoField;
9
10
use CMB2\BrightcoveVideoField;
11
use CMB2_Bootstrap_260;
12
use CMB2_Field;
13
use CMB2_Types;
14
use WP_UnitTestCase;
15
16
/**
17
 * Brightcove Video Field test case.
18
 */
19
class Brightcove_Video_Field extends WP_UnitTestCase {
20
21
	/**
22
	 * Cmb2 Plugin File Path.
23
	 *
24
	 * @var string
25
	 */
26
	static $cmb2_plugin = WP_PLUGIN_DIR . '/cmb2/init.php';
27
28
	/**
29
	 * Brightcove Plugin File Path.
30
	 *
31
	 * @var string
32
	 */
33
	static $brightcove_plugin = WP_PLUGIN_DIR . '/brightcove-video-connect/brightcove-video-connect.php';
34
35
	/**
36
	 * Test to check cmb2 plugin availability.
37
	 */
38
	public function test_cmb2_plugin_exists() {
39
		$this->assertTrue(
40
			file_exists( self::$brightcove_plugin ),
41
			'CMB2 Plugin Not Available.'
42
		);
43
	}
44
45
	/**
46
	 * Test to check brightcove plugin availability.
47
	 */
48
	public function test_brightcove_plugin_exists() {
49
		$this->assertTrue(
50
			file_exists( self::$brightcove_plugin ),
51
			'Brightcove Video Connect Plugin Not Available.'
52
		);
53
	}
54
55
	/**
56
	 * Test bootstrap.
57
	 */
58
	public function test_bootstrap() {
59
		BrightcoveVideoField\bootstrap();
60
61
		$this->assertEquals( 10, has_action( 'plugins_loaded', 'CMB2\\BrightcoveVideoField\\load_textdomain' ) );
62
		$this->assertEquals( 10, has_action( 'plugins_loaded', 'CMB2\\BrightcoveVideoField\\load_plugin' ) );
63
	}
64
65
	/**
66
	 * Test case for admin notice.
67
	 */
68
	public function test_dependency_admin_notice() {
69
		// Actual data.
70
		ob_start();
71
		do_action( 'admin_notices' );
72
		$admin_notices_content = ob_get_contents();
73
		ob_end_clean();
74
75
		$this->assertContains(
76
			htmlentities2( '"CMB2 BrightCove Video Field" plugin can\'t be loaded' ),
77
			$admin_notices_content
78
		);
79
	}
80
81
	/**
82
	 * Test case for plugin load with/without dependency plugins.
83
	 */
84
	public function test_brightcove_video_field_plugin_loaded() {
85
86
		$this->plugin_not_loaded_test_cases();
87
88
		activate_plugin( self::$cmb2_plugin );
89
90
		$this->plugin_not_loaded_test_cases();
91
92
		activate_plugin( self::$brightcove_plugin );
93
94
		$this->maybe_remove_admin_notices();
95
96
		$this->plugin_loaded_test_cases();
97
	}
98
99
	/**
100
	 * Test case for plugin load without dependency plugins.
101
	 */
102
	public function plugin_not_loaded_test_cases() {
103
		BrightcoveVideoField\load_plugin();
104
105
		$this->assertFalse( BrightcoveVideoField\is_dependency_loaded() );
106
107
		$this->assertEquals(
108
			10,
109
			has_action( 'admin_notices', 'CMB2\\BrightcoveVideoField\\dependency_admin_notice' )
110
		);
111
		$this->assertEquals(
112
			10,
113
			has_action( 'network_admin_notices', 'CMB2\\BrightcoveVideoField\\dependency_admin_notice' )
114
		);
115
116
		$this->assertNotEquals(
117
			11,
118
			has_action( 'admin_enqueue_scripts', 'CMB2\\BrightcoveVideoField\\enqueue_scripts' )
119
		);
120
		$this->assertNotEquals(
121
			10,
122
			has_action( 'cmb2_render_brightcove_video', 'CMB2\\BrightcoveVideoField\\cmb2_render_callback_for_brightcove_video' )
123
		);
124
		$this->assertNotEquals(
125
			10,
126
			has_action( 'admin_footer', 'CMB2\\BrightcoveVideoField\\js_wp_templates' )
127
		);
128
	}
129
130
	/**
131
	 * Test case for plugin load with dependency plugins.
132
	 */
133
	public function plugin_loaded_test_cases() {
134
		BrightcoveVideoField\load_plugin();
135
136
		$this->assertTrue( BrightcoveVideoField\is_dependency_loaded() );
137
138
		$this->assertNotEquals(
139
			10,
140
			has_action( 'admin_notices', 'CMB2\\BrightcoveVideoField\\dependency_admin_notice' )
141
		);
142
		$this->assertNotEquals(
143
			10,
144
			has_action( 'network_admin_notices', 'CMB2\\BrightcoveVideoField\\dependency_admin_notice' )
145
		);
146
147
		$this->assertEquals(
148
			11,
149
			has_action( 'admin_enqueue_scripts', 'CMB2\\BrightcoveVideoField\\enqueue_scripts' )
150
		);
151
		$this->assertEquals(
152
			10,
153
			has_action( 'cmb2_render_brightcove_video', 'CMB2\\BrightcoveVideoField\\cmb2_render_callback_for_brightcove_video' )
154
		);
155
		$this->assertEquals(
156
			10,
157
			has_action( 'admin_footer', 'CMB2\\BrightcoveVideoField\\js_wp_templates' )
158
		);
159
	}
160
161
	/**
162
	 * Remove admin notices if already added from `BrightcoveVideoField\load_plugin()`
163
	 * before activating dependency plugins.
164
	 */
165
	public function maybe_remove_admin_notices() {
166
167
		if ( has_action( 'admin_notices', 'CMB2\\BrightcoveVideoField\\dependency_admin_notice' ) ) {
168
			remove_action( 'admin_notices', 'CMB2\\BrightcoveVideoField\\dependency_admin_notice' );
169
		}
170
171
		if ( has_action( 'network_admin_notices', 'CMB2\\BrightcoveVideoField\\dependency_admin_notice' ) ) {
172
			remove_action( 'network_admin_notices', 'CMB2\\BrightcoveVideoField\\dependency_admin_notice' );
173
		}
174
	}
175
176
	/**
177
	 * Test case for enqueue_scripts function.
178
	 */
179
	public function test_enqueue_scripts() {
180
		$script = 'cmb2-brightcove-video-field-js';
181
		$style  = 'cmb2-brightcove-video-field-css';
182
183
		BrightcoveVideoField\enqueue_scripts( '' );
184
185
		$this->assertFalse( wp_script_is( $script ) );
186
		$this->assertFalse( wp_style_is( $style ) );
187
188
		BrightcoveVideoField\enqueue_scripts( 'post.php' );
189
190
		$this->assertTrue( wp_script_is( $script ) );
191
		$this->assertTrue( wp_style_is( $style ) );
192
	}
193
194
	/**
195
	 * Test case for brightcove video field type render.
196
	 */
197
	public function test_cmb2_field_render() {
198
199
		$cmb_plugin_instance = CMB2_Bootstrap_260::initiate();
200
		$cmb_plugin_instance->include_cmb();
201
202
		BrightcoveVideoField\add_brightcove_video_field();
203
204
		$video_data = [
205
			'bc_video_id'       => '1236242437001',
206
			'bc_video_duration' => '2:29',
207
			'bc_player_id'      => '61hrf3oYj',
208
			'bc_account_id'     => '8926567543001',
209
		];
210
211
		$args = [
212
			'name'    => esc_html__( 'Brightcove Video', 'cmb2-brightcove-video-field' ),
213
			'id'      => 'brightcove_featured_video',
214
			'type'    => 'brightcove_video',
215
			'default' => $video_data,
216
		];
217
218
		$cmb_field = new CMB2_Field( [
219
			'field_args' => $args,
220
		] );
221
222
		$field_type_obj = new CMB2_Types( $cmb_field );
223
224
		ob_start();
225
		$field_type_obj->render();
226
		$field_render_html = ob_get_contents();
227
		ob_end_clean();
228
229
		$field_render_html = $this->trim_html_markup( $field_render_html );
230
231
		$hash_id = $cmb_field->hash_id();
232
233
		// Test Brightcove Video Preview.
234
		$this->assertContains(
235
			'https://players.brightcove.net/8926567543001/61hrf3oYj_default/index.html?videoId=1236242437001',
236
			$field_render_html
237
		);
238
239
		$bc_video_id_input_field = sprintf(
240
			'<input
241
				type="hidden"
242
				class="bc_video_id"
243
				name="brightcove_featured_video[bc_video_id]"
244
				id="brightcove_featured_video_bc_video_id"
245
				value="%s"
246
				data-hash=\'%s\' />',
247
			$video_data['bc_video_id'],
248
			$hash_id
249
		);
250
251
		// Test Brightcove Video ID Input Hidden Field.
252
		$this->assertContains(
253
			$this->trim_html_markup( $bc_video_id_input_field ),
254
			$field_render_html
255
		);
256
257
		$bc_video_duration_input_field = sprintf(
258
			'<input
259
				type="hidden"
260
				class="bc_video_duration"
261
				name="brightcove_featured_video[bc_video_duration]"
262
				id="brightcove_featured_video_bc_video_duration"
263
				value="%s"
264
				data-hash=\'%s\' />',
265
			$video_data['bc_video_duration'],
266
			$hash_id
267
		);
268
269
		// Test Brightcove Video Duration Input Hidden Field.
270
		$this->assertContains(
271
			$this->trim_html_markup( $bc_video_duration_input_field ),
272
			$field_render_html
273
		);
274
275
		$bc_player_id_input_field = sprintf(
276
			'<input
277
				type="hidden"
278
				class="bc_player_id"
279
				name="brightcove_featured_video[bc_player_id]"
280
				id="brightcove_featured_video_bc_player_id"
281
				value="%s"
282
				data-hash=\'%s\' />',
283
			$video_data['bc_player_id'],
284
			$hash_id
285
		);
286
287
		// Test Brightcove Video Player ID Input Hidden Field.
288
		$this->assertContains(
289
			$this->trim_html_markup( $bc_player_id_input_field ),
290
			$field_render_html
291
		);
292
293
		$bc_account_id_input_field = sprintf(
294
			'<input
295
				type="hidden"
296
				class="bc_account_id"
297
				name="brightcove_featured_video[bc_account_id]"
298
				id="brightcove_featured_video_bc_account_id"
299
				value="%s"
300
				data-hash=\'%s\' />',
301
			$video_data['bc_account_id'],
302
			$hash_id
303
		);
304
305
		// Test Brightcove Account ID Input Hidden Field.
306
		$this->assertContains(
307
			$this->trim_html_markup( $bc_account_id_input_field ),
308
			$field_render_html
309
		);
310
	}
311
312
	/**
313
	 * Remove extra spaces from html output of ob_get_content.
314
	 *
315
	 * @param string $content ob_get_content data.
316
	 *
317
	 * @return string|null clean ob data.
318
	 */
319
	public function trim_html_markup( $content ) {
320
321
		// Remove any extra space from ob_start.
322
		$content = preg_replace( '/\s+/', ' ', $content );
323
324
		return trim( $content );
325
	}
326
327
	/**
328
	 * Test case for js_wp_templates function.
329
	 */
330
	public function test_js_wp_templates() {
331
		// Actual data.
332
		ob_start();
333
		BrightcoveVideoField\js_wp_templates();
334
		$cmb2_bc_video_preview = ob_get_contents();
335
		ob_end_clean();
336
337
		$this->assertContains(
338
			'<script type="text/html" id="tmpl-cmb2-brightcove-video-preview">',
339
			$cmb2_bc_video_preview
340
		);
341
342
		$this->assertContains(
343
			'https://players.brightcove.net/{{data.account_id}}/{{data.player_id}}_default/index.html?videoId={{data.id}}',
344
			$cmb2_bc_video_preview
345
		);
346
	}
347
348
}
349