Skip to content

Commit e0ab68b

Browse files
committed
Added tests for the Settings_Page class. Slight refactor to help with making the code testable.
1 parent dd47988 commit e0ab68b

File tree

3 files changed

+149
-79
lines changed

3 files changed

+149
-79
lines changed

plugins/hwp-previews/src/Admin/Settings_Page.php

Lines changed: 56 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@ class Settings_Page {
4343
public function __construct() {
4444
$this->parameters = Preview_Parameter_Registry::get_instance();
4545
$this->post_preview_service = new Post_Preview_Service();
46-
47-
$this->register_settings_pages();
48-
$this->register_settings_fields();
49-
$this->load_scripts_styles();
5046
}
5147

5248
/**
@@ -58,6 +54,7 @@ public static function init(): ?Settings_Page {
5854
}
5955
if ( ! isset( self::$instance ) || ! ( is_a( self::$instance, self::class ) ) ) {
6056
self::$instance = new self();
57+
self::$instance->setup();
6158
}
6259

6360
/**
@@ -70,45 +67,50 @@ public static function init(): ?Settings_Page {
7067
return self::$instance;
7168
}
7269

70+
/**
71+
* Sets up the settings page by registering hooks.
72+
*/
73+
public function setup(): void {
74+
add_action( 'admin_menu', [ $this, 'register_settings_page' ], 10, 0 );
75+
add_action( 'admin_init', [ $this, 'register_settings_fields' ], 10, 0 );
76+
add_action( 'admin_enqueue_scripts', [ $this, 'load_scripts_styles' ], 10, 1 );
77+
}
78+
7379
/**
7480
* Registers the settings page.
7581
*/
76-
public function register_settings_pages(): void {
77-
add_action( 'admin_menu', function (): void {
78-
79-
// Note: We didn't initalise in the constructor because we need to ensure
80-
// the post-types are registered before we can use them.
81-
$post_types = $this->post_preview_service->get_post_types();
82-
83-
$page = new Menu_Page(
84-
__( 'HWP Previews Settings', 'hwp-previews' ),
85-
'HWP Previews',
86-
self::PLUGIN_MENU_SLUG,
87-
trailingslashit( HWP_PREVIEWS_PLUGIN_DIR ) . 'src/Templates/admin.php',
88-
[
89-
'hwp_previews_main_page_config' => [
90-
'tabs' => $post_types,
91-
'current_tab' => $this->get_current_tab( $post_types ),
92-
'params' => $this->parameters->get_descriptions(),
93-
],
82+
public function register_settings_page(): void {
83+
84+
// Note: We didn't initalise in the constructor because we need to ensure
85+
// the post-types are registered before we can use them.
86+
$post_types = $this->post_preview_service->get_post_types();
87+
88+
$page = new Menu_Page(
89+
__( 'HWP Previews Settings', 'hwp-previews' ),
90+
'HWP Previews',
91+
self::PLUGIN_MENU_SLUG,
92+
trailingslashit( HWP_PREVIEWS_PLUGIN_DIR ) . 'src/Templates/admin.php',
93+
[
94+
'hwp_previews_main_page_config' => [
95+
'tabs' => $post_types,
96+
'current_tab' => $this->get_current_tab( $post_types ),
97+
'params' => $this->parameters->get_descriptions(),
9498
],
95-
);
99+
],
100+
);
96101

97-
$page->register_page();
98-
} );
102+
$page->register_page();
99103
}
100104

101105
/**
102106
* Registers the settings fields for each post type.
103107
*/
104108
public function register_settings_fields(): void {
105-
add_action( 'admin_init', function (): void {
106-
$settings_manager = new Settings_Form_Manager(
107-
$this->post_preview_service->get_post_types(),
108-
new Settings_Field_Collection()
109-
);
110-
$settings_manager->render_form();
111-
}, 10, 0 );
109+
$settings_manager = new Settings_Form_Manager(
110+
$this->post_preview_service->get_post_types(),
111+
new Settings_Field_Collection()
112+
);
113+
$settings_manager->render_form();
112114
}
113115

114116
/**
@@ -133,28 +135,28 @@ public function get_current_tab( array $post_types, string $tab = 'tab' ): strin
133135

134136
/**
135137
* Enqueues the JavaScript and the CSS file for the plugin admin area.
138+
*
139+
* @param string $hook The current admin page hook.
136140
*/
137-
public function load_scripts_styles(): void {
138-
add_action( 'admin_enqueue_scripts', static function ( string $hook ): void {
139-
140-
if ( 'settings_page_' . self::PLUGIN_MENU_SLUG !== $hook ) {
141-
return;
142-
}
143-
144-
wp_enqueue_script(
145-
'hwp-previews-js',
146-
trailingslashit( HWP_PREVIEWS_PLUGIN_URL ) . 'assets/js/hwp-previews.js',
147-
[],
148-
HWP_PREVIEWS_VERSION,
149-
true
150-
);
151-
152-
wp_enqueue_style(
153-
'hwp-previews-css',
154-
trailingslashit( HWP_PREVIEWS_PLUGIN_URL ) . 'assets/css/hwp-previews.css',
155-
[],
156-
HWP_PREVIEWS_VERSION
157-
);
158-
} );
141+
public function load_scripts_styles( string $hook ): void {
142+
143+
if ( 'settings_page_' . self::PLUGIN_MENU_SLUG !== $hook ) {
144+
return;
145+
}
146+
147+
wp_enqueue_script(
148+
'hwp-previews-js',
149+
trailingslashit( HWP_PREVIEWS_PLUGIN_URL ) . 'assets/js/hwp-previews.js',
150+
[],
151+
HWP_PREVIEWS_VERSION,
152+
true
153+
);
154+
155+
wp_enqueue_style(
156+
'hwp-previews-css',
157+
trailingslashit( HWP_PREVIEWS_PLUGIN_URL ) . 'assets/css/hwp-previews.css',
158+
[],
159+
HWP_PREVIEWS_VERSION
160+
);
159161
}
160162
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace HWP\Previews\wpunit\Admin;
4+
5+
use HWP\Previews\Admin\Settings_Page;
6+
use HWP\Previews\Preview\Post\Post_Preview_Service;
7+
use lucatume\WPBrowser\TestCase\WPTestCase;
8+
use ReflectionClass;
9+
10+
class SettingsPageTest extends WPTestCase {
11+
12+
public function set_as_admin() {
13+
// Will set is_admin() to true
14+
$GLOBALS['current_screen'] = new class {
15+
public function in_admin( $context = null ) {
16+
if ( $context === null ) {
17+
return true;
18+
}
19+
20+
return $context === 'user';
21+
}
22+
};
23+
}
24+
25+
public function unset_as_admin() {
26+
unset( $GLOBALS['current_screen'] );
27+
}
28+
29+
public function test_settings_page_instance() {
30+
31+
$this->set_as_admin();
32+
33+
$reflection = new ReflectionClass( Settings_Page::class );
34+
$instanceProperty = $reflection->getProperty( 'instance' );
35+
$instanceProperty->setAccessible( true );
36+
$instanceProperty->setValue( null );
37+
38+
$this->assertNull( $instanceProperty->getValue() );
39+
$instance = Settings_Page::init();
40+
41+
$this->assertInstanceOf( Settings_Page::class, $instanceProperty->getValue() );
42+
$this->assertSame( $instance, $instanceProperty->getValue(), 'Settings_Page::init() should set the static instance property' );
43+
$this->unset_as_admin();
44+
}
45+
46+
public function test_get_current_tab() {
47+
$this->set_as_admin();
48+
$_GET['attachment'] = 'attachment';
49+
$settings_page = Settings_Page::init();
50+
51+
$post_preview_service = new Post_Preview_Service();
52+
$post_types = $post_preview_service->get_post_types();
53+
54+
55+
$tab = $settings_page->get_current_tab( [], 'attachment' );
56+
$this->assertSame( '', $tab );
57+
58+
$tab = $settings_page->get_current_tab( $post_types, 'page' );
59+
$this->assertEquals( 'post', $tab );
60+
61+
$tab = $settings_page->get_current_tab( $post_types, 'attachment' );
62+
$this->assertSame( 'attachment', $tab );
63+
}
64+
65+
public function test_register_hooks() {
66+
$settings_page = new Settings_Page();
67+
$this->assertNull( $settings_page->register_settings_page() );
68+
$this->assertNull( $settings_page->register_settings_fields() );
69+
$this->assertNull( $settings_page->load_scripts_styles( 'settings_page_hwp-previews' ) );
70+
}
71+
}

plugins/hwp-previews/tests/wpunit/Hooks/PreviewHooksTest.php

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
22

33
namespace HWP\Previews\wpunit\Hooks;
44

5-
use http\Env\Url;
65
use HWP\Previews\Admin\Settings\Fields\Settings_Field_Collection;
76
use HWP\Previews\Hooks\Preview_Hooks;
87
use HWP\Previews\Preview\Template\Template_Resolver_Service;
98
use lucatume\WPBrowser\TestCase\WPTestCase;
10-
119
use ReflectionClass;
12-
use WP_Mock;
1310
use WP_Post;
1411
use WP_REST_Response;
1512

@@ -490,23 +487,23 @@ public function test_filter_rest_prepare_link_adds_link() {
490487

491488
$test_config = [
492489
'post' => [
493-
Settings_Field_Collection::ENABLED_FIELD_ID => true,
494-
Settings_Field_Collection::IN_IFRAME_FIELD_ID => false,
490+
Settings_Field_Collection::ENABLED_FIELD_ID => true,
491+
Settings_Field_Collection::IN_IFRAME_FIELD_ID => false,
495492
Settings_Field_Collection::PREVIEW_URL_FIELD_ID => $preview_link,
496493
]
497494
];
498495
update_option( $this->test_option_key, $test_config );
499496

500497
$new_post = $this->post;
501498

502-
$original_response = new WP_REST_Response(['foo' => 'bar']);
503-
$preview = new Preview_Hooks();
499+
$original_response = new WP_REST_Response( [ 'foo' => 'bar' ] );
500+
$preview = new Preview_Hooks();
504501

505502
$response = $preview->filter_rest_prepare_link( $original_response, $new_post );
506-
$data = $response->get_data();
503+
$data = $response->get_data();
507504
$this->assertArrayHasKey( 'link', $data );
508505

509-
$this->assertEquals( 'https://localhost:3000/post?preview=true&post_id=' . $new_post->ID . '&status=' . $new_post->post_status, $data[ 'link' ] );
506+
$this->assertEquals( 'https://localhost:3000/post?preview=true&post_id=' . $new_post->ID . '&status=' . $new_post->post_status, $data['link'] );
510507
}
511508

512509
public function test_filter_rest_prepare_link_no_link_iframe_enabled() {
@@ -515,22 +512,22 @@ public function test_filter_rest_prepare_link_no_link_iframe_enabled() {
515512

516513
$test_config = [
517514
'post' => [
518-
Settings_Field_Collection::ENABLED_FIELD_ID => true,
519-
Settings_Field_Collection::IN_IFRAME_FIELD_ID => true,
515+
Settings_Field_Collection::ENABLED_FIELD_ID => true,
516+
Settings_Field_Collection::IN_IFRAME_FIELD_ID => true,
520517
Settings_Field_Collection::PREVIEW_URL_FIELD_ID => $preview_link,
521518
]
522519
];
523520
update_option( $this->test_option_key, $test_config );
524521

525522
$new_post = $this->post;
526523

527-
$original_response = new WP_REST_Response(['foo' => 'bar']);
528-
$preview = new Preview_Hooks();
524+
$original_response = new WP_REST_Response( [ 'foo' => 'bar' ] );
525+
$preview = new Preview_Hooks();
529526

530527
$response = $preview->filter_rest_prepare_link( $original_response, $new_post );
531-
$data = $response->get_data();
528+
$data = $response->get_data();
532529
$this->assertArrayNotHasKey( 'link', $data );
533-
$this->assertEquals($original_response, $response);
530+
$this->assertEquals( $original_response, $response );
534531
}
535532

536533
public function test_filter_rest_prepare_link_no_link_previews_not_enabled() {
@@ -539,22 +536,22 @@ public function test_filter_rest_prepare_link_no_link_previews_not_enabled() {
539536

540537
$test_config = [
541538
'post' => [
542-
Settings_Field_Collection::ENABLED_FIELD_ID => false,
543-
Settings_Field_Collection::IN_IFRAME_FIELD_ID => false,
539+
Settings_Field_Collection::ENABLED_FIELD_ID => false,
540+
Settings_Field_Collection::IN_IFRAME_FIELD_ID => false,
544541
Settings_Field_Collection::PREVIEW_URL_FIELD_ID => $preview_link,
545542
]
546543
];
547544
update_option( $this->test_option_key, $test_config );
548545

549546
$new_post = $this->post;
550547

551-
$original_response = new WP_REST_Response(['foo' => 'bar']);
552-
$preview = new Preview_Hooks();
548+
$original_response = new WP_REST_Response( [ 'foo' => 'bar' ] );
549+
$preview = new Preview_Hooks();
553550

554551
$response = $preview->filter_rest_prepare_link( $original_response, $new_post );
555-
$data = $response->get_data();
552+
$data = $response->get_data();
556553
$this->assertArrayNotHasKey( 'link', $data );
557-
$this->assertEquals($original_response, $response);
554+
$this->assertEquals( $original_response, $response );
558555
}
559556

560557
public function test_filter_rest_prepare_link_no_link_previews_no_preview_url() {
@@ -569,12 +566,12 @@ public function test_filter_rest_prepare_link_no_link_previews_no_preview_url()
569566

570567
$new_post = $this->post;
571568

572-
$original_response = new WP_REST_Response(['foo' => 'bar']);
573-
$preview = new Preview_Hooks();
569+
$original_response = new WP_REST_Response( [ 'foo' => 'bar' ] );
570+
$preview = new Preview_Hooks();
574571

575572
$response = $preview->filter_rest_prepare_link( $original_response, $new_post );
576-
$data = $response->get_data();
573+
$data = $response->get_data();
577574
$this->assertArrayNotHasKey( 'link', $data );
578-
$this->assertEquals($original_response, $response);
575+
$this->assertEquals( $original_response, $response );
579576
}
580577
}

0 commit comments

Comments
 (0)