@@ -44,4 +44,157 @@ function test_domain_hash() {
4444 $ this ->assertEquals ( $ this ->to_domain_hash ("//www.domain.com/ " ), $ value );
4545 $ this ->assertNotEquals ( $ this ->to_domain_hash ("https://something.com/ " ), $ value );
4646 }
47+
48+ /**
49+ * Test get_unoptimized_url with non-Optimole URLs.
50+ */
51+ function test_get_unoptimized_url_non_optimole () {
52+ // Initialize config for testing
53+ $ settings = new Optml_Settings ();
54+ $ settings ->update ( 'service_data ' , [
55+ 'cdn_key ' => 'test123 ' ,
56+ 'cdn_secret ' => '12345 ' ,
57+ 'whitelist ' => [ 'example.com ' , 'example.org ' ],
58+ ] );
59+ Optml_Config::init ( [
60+ 'key ' => 'test123 ' ,
61+ 'secret ' => '12345 ' ,
62+ ] );
63+
64+ // Non-Optimole URLs should be returned as-is
65+ $ url = 'http://example.org/wp-content/uploads/image.jpg ' ;
66+ $ this ->assertEquals ( $ url , $ this ->get_unoptimized_url ( $ url ) );
67+
68+ $ url = 'https://example.com/image.png ' ;
69+ $ this ->assertEquals ( $ url , $ this ->get_unoptimized_url ( $ url ) );
70+
71+ $ url = '/wp-content/uploads/image.jpg ' ;
72+ $ this ->assertEquals ( $ url , $ this ->get_unoptimized_url ( $ url ) );
73+ }
74+
75+ /**
76+ * Test get_unoptimized_url with Optimole URLs (regular images).
77+ */
78+ function test_get_unoptimized_url_optimole_regular () {
79+ // Initialize config for testing
80+ $ settings = new Optml_Settings ();
81+ $ settings ->update ( 'service_data ' , [
82+ 'cdn_key ' => 'test123 ' ,
83+ 'cdn_secret ' => '12345 ' ,
84+ 'whitelist ' => [ 'example.com ' , 'example.org ' ],
85+ ] );
86+ Optml_Config::init ( [
87+ 'key ' => 'test123 ' ,
88+ 'secret ' => '12345 ' ,
89+ ] );
90+
91+ // Optimole URL with regular image - should extract original URL after second 'http'
92+ $ optimole_url = 'https://test123.i.optimole.com/cb:test/w:800/h:600/q:mauto/http://example.org/wp-content/uploads/image.jpg ' ;
93+ $ expected = 'http://example.org/wp-content/uploads/image.jpg ' ;
94+ $ this ->assertEquals ( $ expected , $ this ->get_unoptimized_url ( $ optimole_url ) );
95+
96+ // Optimole URL with https in original
97+ $ optimole_url = 'https://test123.i.optimole.com/cb:test/w:800/h:600/q:mauto/https://example.org/wp-content/uploads/image.jpg ' ;
98+ $ expected = 'https://example.org/wp-content/uploads/image.jpg ' ;
99+ $ this ->assertEquals ( $ expected , $ this ->get_unoptimized_url ( $ optimole_url ) );
100+
101+ // Optimole URL with query parameters
102+ $ optimole_url = 'https://test123.i.optimole.com/cb:test/w:800/h:600/q:mauto/http://example.org/wp-content/uploads/image.jpg?param=value ' ;
103+ $ expected = 'http://example.org/wp-content/uploads/image.jpg?param=value ' ;
104+ $ this ->assertEquals ( $ expected , $ this ->get_unoptimized_url ( $ optimole_url ) );
105+ }
106+
107+ /**
108+ * Test get_unoptimized_url with uploaded images (offloaded images).
109+ */
110+ function test_get_unoptimized_url_uploaded_image () {
111+ // Initialize config for testing
112+ $ settings = new Optml_Settings ();
113+ $ settings ->update ( 'service_data ' , [
114+ 'cdn_key ' => 'test123 ' ,
115+ 'cdn_secret ' => '12345 ' ,
116+ 'whitelist ' => [ 'example.com ' , 'example.org ' ],
117+ ] );
118+ Optml_Config::init ( [
119+ 'key ' => 'test123 ' ,
120+ 'secret ' => '12345 ' ,
121+ ] );
122+
123+ // Uploaded image URL with /id: pattern - should extract original URL
124+ $ uploaded_url = 'https://test123.i.optimole.com/cb:test/w:800/h:600/q:mauto/id:abc123/http://example.org/wp-content/uploads/image.jpg ' ;
125+ $ expected = '/id:abc123/http://example.org/wp-content/uploads/image.jpg ' ;
126+ $ result = $ this ->get_unoptimized_url ( $ uploaded_url );
127+ $ this ->assertStringContainsString ( '/id:abc123/ ' , $ result );
128+ $ this ->assertStringContainsString ( 'http://example.org/wp-content/uploads/image.jpg ' , $ result );
129+
130+ // Uploaded image URL with https in original
131+ $ uploaded_url = 'https://test123.i.optimole.com/cb:test/w:800/h:600/q:mauto/id:xyz789/https://example.org/wp-content/uploads/image.jpg ' ;
132+ $ expected = '/id:xyz789/https://example.org/wp-content/uploads/image.jpg ' ;
133+ $ result = $ this ->get_unoptimized_url ( $ uploaded_url );
134+ $ this ->assertStringContainsString ( '/id:xyz789/ ' , $ result );
135+ $ this ->assertStringContainsString ( 'https://example.org/wp-content/uploads/image.jpg ' , $ result );
136+ }
137+
138+ /**
139+ * Test get_unoptimized_url edge cases.
140+ */
141+ function test_get_unoptimized_url_edge_cases () {
142+ // Initialize config for testing
143+ $ settings = new Optml_Settings ();
144+ $ settings ->update ( 'service_data ' , [
145+ 'cdn_key ' => 'test123 ' ,
146+ 'cdn_secret ' => '12345 ' ,
147+ 'whitelist ' => [ 'example.com ' , 'example.org ' ],
148+ ] );
149+ Optml_Config::init ( [
150+ 'key ' => 'test123 ' ,
151+ 'secret ' => '12345 ' ,
152+ ] );
153+
154+ // Optimole URL without second 'http' - should return as-is
155+ $ optimole_url = 'https://test123.i.optimole.com/cb:test/w:800/h:600/q:mauto ' ;
156+ $ this ->assertEquals ( $ optimole_url , $ this ->get_unoptimized_url ( $ optimole_url ) );
157+
158+ // Empty string
159+ $ this ->assertEquals ( '' , $ this ->get_unoptimized_url ( '' ) );
160+
161+ // URL with only one 'http' occurrence
162+ $ optimole_url = 'https://test123.i.optimole.com/cb:test/w:800/h:600/q:mauto/image.jpg ' ;
163+ $ this ->assertEquals ( $ optimole_url , $ this ->get_unoptimized_url ( $ optimole_url ) );
164+
165+ // Uploaded image URL without matching pattern - should return as-is
166+ $ uploaded_url = 'https://test123.i.optimole.com/cb:test/w:800/h:600/q:mauto/id:abc123/image.jpg ' ;
167+ $ result = $ this ->get_unoptimized_url ( $ uploaded_url );
168+ // Should return the URL as-is since pattern doesn't match
169+ $ this ->assertEquals ( $ uploaded_url , $ result );
170+ }
171+
172+ /**
173+ * Test get_unoptimized_url with custom domain configuration.
174+ */
175+ function test_get_unoptimized_url_custom_domain () {
176+ // Initialize config with custom domain
177+ $ settings = new Optml_Settings ();
178+ $ settings ->update ( 'service_data ' , [
179+ 'cdn_key ' => 'test123 ' ,
180+ 'cdn_secret ' => '12345 ' ,
181+ 'whitelist ' => [ 'example.com ' , 'example.org ' ],
182+ 'domain ' => 'cdn.example.com ' ,
183+ 'is_cname_assigned ' => 'yes ' ,
184+ ] );
185+ Optml_Config::init ( [
186+ 'key ' => 'test123 ' ,
187+ 'secret ' => '12345 ' ,
188+ 'domain ' => 'cdn.example.com ' ,
189+ ] );
190+
191+ // Custom domain Optimole URL
192+ $ optimole_url = 'https://cdn.example.com/cb:test/w:800/h:600/q:mauto/http://example.org/wp-content/uploads/image.jpg ' ;
193+ $ expected = 'http://example.org/wp-content/uploads/image.jpg ' ;
194+ $ this ->assertEquals ( $ expected , $ this ->get_unoptimized_url ( $ optimole_url ) );
195+
196+ // Non-Optimole URL should still return as-is
197+ $ url = 'http://example.org/wp-content/uploads/image.jpg ' ;
198+ $ this ->assertEquals ( $ url , $ this ->get_unoptimized_url ( $ url ) );
199+ }
47200}
0 commit comments