44
55namespace HWP \Previews \Tests \Unit \Preview \Template ;
66
7+ use HWP \Previews \Preview \Parameter \Preview_Parameter ;
78use HWP \Previews \Preview \Parameter \Preview_Parameter_Registry ;
89use HWP \Previews \Preview \Url \Preview_Url_Resolver_Service ;
910use lucatume \WPBrowser \TestCase \WPTestCase ;
11+ use WP_Post ;
1012
1113class Preview_Url_Resolver_Service_Test extends WPTestCase {
1214
@@ -20,5 +22,140 @@ public function test_class_instance(): void {
2022 $ this ->assertInstanceOf (Preview_Url_Resolver_Service::class, $ service );
2123 }
2224
23- // @TODO add more tests for resolve method
25+ /**
26+ * Main logic for preview URL resolution.
27+ *
28+ * @return void
29+ */
30+ public function test_resolve_default_parameters (): void {
31+
32+ $ registry = Preview_Parameter_Registry::get_instance ();
33+ $ service = new Preview_Url_Resolver_Service ($ registry );
34+
35+ $ author = WPTestCase::factory ()->user ->create_and_get ( [
36+ 'user_login ' => 'test_author ' ,
37+ 'user_email ' => uniqid ( 'test_author ' , true ) . '@example.com '
38+ ]);
39+
40+ $ post = WPTestCase::factory ()->post ->create_and_get ( [
41+ 'post_title ' => 'Test Post ' ,
42+ 'post_status ' => 'publish ' ,
43+ 'post_content ' => 'This is a test post. ' ,
44+ 'post_type ' => 'page ' , // Using this as it can be hierarchical
45+ 'post_author ' => $ author ->ID ,
46+ 'post_date ' => '2023-10-01 12:00:00 ' ,
47+ 'post_date_gmt ' => '2023-10-01 12:00:00 ' ,
48+ 'post_modified ' => '2023-10-01 12:00:00 ' ,
49+ ] );
50+
51+ $ this ->assertEquals (
52+ $ service ->resolve ($ post , 'https://localhost:3000/preview={ID} ' ),
53+ 'https://localhost:3000/preview= ' . $ post ->ID
54+ );
55+
56+ $ this ->assertEquals (
57+ $ service ->resolve ($ post , 'https://localhost:3000/preview={author_ID} ' ),
58+ 'https://localhost:3000/preview= ' . $ author ->ID
59+ );
60+
61+ $ this ->assertEquals (
62+ $ service ->resolve ($ post , 'https://localhost:3000/preview={status} ' ),
63+ 'https://localhost:3000/preview= ' . $ post ->post_status
64+ );
65+
66+ $ this ->assertEquals (
67+ $ service ->resolve ($ post , 'https://localhost:3000/preview={type} ' ),
68+ 'https://localhost:3000/preview= ' . $ post ->post_type
69+ );
70+
71+ $ this ->assertEquals (
72+ $ service ->resolve ($ post , 'https://localhost:3000/preview={template} ' ),
73+ 'https://localhost:3000/preview= ' . get_page_template_slug ( $ post )
74+ );
75+
76+ // Asserting the parent post ID is 0 as we do not set a parent post.
77+ $ this ->assertEquals (
78+ $ service ->resolve ($ post , 'https://localhost:3000/preview={parent_ID} ' ),
79+ 'https://localhost:3000/preview=0 '
80+ );
81+
82+
83+ $ child_post = WPTestCase::factory ()->post ->create_and_get ( [
84+ 'post_title ' => 'Child Post ' ,
85+ 'post_status ' => 'publish ' ,
86+ 'post_content ' => 'This is a child post. ' ,
87+ 'post_type ' => 'page ' , // Using this as it can be hierarchical
88+ 'post_author ' => $ author ->ID ,
89+ 'post_parent ' => $ post ->ID , // Setting the parent post
90+ 'post_date ' => '2023-10-01 12:00:00 ' ,
91+ 'post_date_gmt ' => '2023-10-01 12:00:00 ' ,
92+ 'post_modified ' => '2023-10-01 12:00:00 ' ,
93+ ] );
94+
95+ $ this ->assertEquals (
96+ $ service ->resolve ($ child_post , 'https://localhost:3000/preview={parent_ID} ' ),
97+ 'https://localhost:3000/preview= ' . $ post ->ID
98+ );
99+
100+ }
101+
102+
103+ public function test_custom_parameters_resolution () {
104+
105+ $ registry = Preview_Parameter_Registry::get_instance ();
106+ $ service = new Preview_Url_Resolver_Service ($ registry );
107+
108+ $ author = WPTestCase::factory ()->user ->create_and_get ( [
109+ 'user_login ' => 'test_author ' ,
110+ 'user_email ' => uniqid ( 'test_author ' , true ) . '@example.com '
111+ ]);
112+
113+ $ post = WPTestCase::factory ()->post ->create_and_get ( [
114+ 'post_title ' => 'Test Post ' ,
115+ 'post_status ' => 'publish ' ,
116+ 'post_content ' => 'This is a test post. ' ,
117+ 'post_type ' => 'page ' , // Using this as it can be hierarchical
118+ 'post_author ' => $ author ->ID ,
119+ 'post_date ' => '2023-10-01 12:00:00 ' ,
120+ 'post_date_gmt ' => '2023-10-01 12:00:00 ' ,
121+ 'post_modified ' => '2023-10-01 12:00:00 ' ,
122+ ] );
123+
124+ $ registry ->register (new Preview_Parameter ('custom_param ' , static fn (WP_Post $ post ) => 'custom_value ' , 'A custom parameter for testing. ' ));
125+
126+ $ this ->assertEquals (
127+ $ service ->resolve ($ post , 'https://localhost:3000/preview={custom_param} ' ),
128+ 'https://localhost:3000/preview=custom_value '
129+ );
130+ }
131+
132+ public function test_custom_parameters_resolution_no_registered_class_returns_placeholder () {
133+
134+ $ registry = Preview_Parameter_Registry::get_instance ();
135+ $ service = new Preview_Url_Resolver_Service ($ registry );
136+
137+ $ author = WPTestCase::factory ()->user ->create_and_get ( [
138+ 'user_login ' => 'test_author ' ,
139+ 'user_email ' => uniqid ( 'test_author ' , true ) . '@example.com '
140+ ]);
141+
142+ $ post = WPTestCase::factory ()->post ->create_and_get ( [
143+ 'post_title ' => 'Test Post ' ,
144+ 'post_status ' => 'publish ' ,
145+ 'post_content ' => 'This is a test post. ' ,
146+ 'post_type ' => 'page ' , // Using this as it can be hierarchical
147+ 'post_author ' => $ author ->ID ,
148+ 'post_date ' => '2023-10-01 12:00:00 ' ,
149+ 'post_date_gmt ' => '2023-10-01 12:00:00 ' ,
150+ 'post_modified ' => '2023-10-01 12:00:00 ' ,
151+ ] );
152+
153+ // Ensure the custom parameter is not registered
154+ $ registry ->unregister ('custom_param ' );
155+
156+ $ this ->assertEquals (
157+ $ service ->resolve ($ post , 'https://localhost:3000/preview={custom_param} ' ),
158+ 'https://localhost:3000/preview= ' . $ service ::PLACEHOLDER_NOT_FOUND
159+ );
160+ }
24161}
0 commit comments