-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContentProxyTest.php
More file actions
994 lines (816 loc) · 33.2 KB
/
ContentProxyTest.php
File metadata and controls
994 lines (816 loc) · 33.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
<?php
/**
* Tests for ExeLearning_Content_Proxy class.
*
* @package Exelearning
*/
/**
* Class ContentProxyTest.
*
* @covers ExeLearning_Content_Proxy
*/
class ContentProxyTest extends WP_UnitTestCase {
/**
* Test instance.
*
* @var ExeLearning_Content_Proxy
*/
private $proxy;
/**
* Set up test fixtures.
*/
public function set_up() {
parent::set_up();
$this->proxy = new ExeLearning_Content_Proxy();
}
/**
* Test get_proxy_url returns correct URL for valid hash.
*/
public function test_get_proxy_url_returns_correct_url() {
$hash = str_repeat( 'a', 40 );
$url = ExeLearning_Content_Proxy::get_proxy_url( $hash );
$this->assertStringContainsString( 'exelearning/v1/content/', $url );
$this->assertStringContainsString( $hash, $url );
$this->assertStringContainsString( 'index.html', $url );
}
/**
* Test get_proxy_url with custom file parameter.
*/
public function test_get_proxy_url_with_custom_file() {
$hash = str_repeat( 'b', 40 );
$url = ExeLearning_Content_Proxy::get_proxy_url( $hash, 'styles/main.css' );
$this->assertStringContainsString( $hash, $url );
$this->assertStringContainsString( 'styles/main.css', $url );
}
/**
* Test get_proxy_url returns null for empty hash.
*/
public function test_get_proxy_url_returns_null_for_empty_hash() {
$this->assertNull( ExeLearning_Content_Proxy::get_proxy_url( '' ) );
$this->assertNull( ExeLearning_Content_Proxy::get_proxy_url( null ) );
}
/**
* Test sanitize_path blocks directory traversal attempts.
*/
public function test_sanitize_path_blocks_directory_traversal() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'sanitize_path' );
$method->setAccessible( true );
// These should return null (blocked).
$this->assertNull( $method->invoke( $this->proxy, '../../../etc/passwd' ) );
$this->assertNull( $method->invoke( $this->proxy, 'folder/../../../secret' ) );
$this->assertNull( $method->invoke( $this->proxy, '..\\..\\windows\\system32' ) );
}
/**
* Test sanitize_path removes null bytes.
*/
public function test_sanitize_path_removes_null_bytes() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'sanitize_path' );
$method->setAccessible( true );
$result = $method->invoke( $this->proxy, "file\0.php" );
$this->assertStringNotContainsString( "\0", $result );
$this->assertEquals( 'file.php', $result );
}
/**
* Test sanitize_path normalizes slashes.
*/
public function test_sanitize_path_normalizes_slashes() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'sanitize_path' );
$method->setAccessible( true );
$result = $method->invoke( $this->proxy, 'folder\\subfolder\\file.html' );
$this->assertEquals( 'folder/subfolder/file.html', $result );
}
/**
* Test sanitize_path returns index.html for empty path.
*/
public function test_sanitize_path_returns_index_for_empty() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'sanitize_path' );
$method->setAccessible( true );
$this->assertEquals( 'index.html', $method->invoke( $this->proxy, '' ) );
$this->assertEquals( 'index.html', $method->invoke( $this->proxy, '/' ) );
$this->assertEquals( 'index.html', $method->invoke( $this->proxy, './' ) );
}
/**
* Test sanitize_path handles URL encoded paths.
*/
public function test_sanitize_path_decodes_url_encoding() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'sanitize_path' );
$method->setAccessible( true );
$result = $method->invoke( $this->proxy, 'folder%2Ffile%2Ehtml' );
$this->assertEquals( 'folder/file.html', $result );
}
/**
* Test sanitize_path rejects encoded directory traversal.
*/
public function test_sanitize_path_rejects_encoded_traversal() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'sanitize_path' );
$method->setAccessible( true );
// %2e%2e = ..
$this->assertNull( $method->invoke( $this->proxy, '%2e%2e/%2e%2e/secret' ) );
}
/**
* Test serve_content rejects invalid hash format.
*/
public function test_serve_content_rejects_invalid_hash() {
// Create a mock REST request with invalid hash.
$request = new WP_REST_Request( 'GET', '/exelearning/v1/content/invalid-hash/index.html' );
$request->set_param( 'hash', 'invalid-hash' );
$request->set_param( 'file', 'index.html' );
$result = $this->proxy->serve_content( $request );
$this->assertInstanceOf( WP_Error::class, $result );
$this->assertEquals( 'invalid_hash', $result->get_error_code() );
}
/**
* Test serve_content rejects hash that is too short.
*/
public function test_serve_content_rejects_short_hash() {
$request = new WP_REST_Request( 'GET', '/exelearning/v1/content/abc123/index.html' );
$request->set_param( 'hash', 'abc123' );
$result = $this->proxy->serve_content( $request );
$this->assertInstanceOf( WP_Error::class, $result );
$this->assertEquals( 'invalid_hash', $result->get_error_code() );
}
/**
* Test serve_content rejects hash with invalid characters.
*/
public function test_serve_content_rejects_hash_with_invalid_chars() {
$request = new WP_REST_Request( 'GET', '/exelearning/v1/content/test/index.html' );
// Hash with special characters.
$request->set_param( 'hash', str_repeat( 'g', 40 ) ); // 'g' is not hex.
$result = $this->proxy->serve_content( $request );
$this->assertInstanceOf( WP_Error::class, $result );
$this->assertEquals( 'invalid_hash', $result->get_error_code() );
}
/**
* Test serve_content returns file not found for non-existent file.
*/
public function test_serve_content_returns_not_found_for_missing_file() {
$request = new WP_REST_Request( 'GET', '/exelearning/v1/content/test/index.html' );
$request->set_param( 'hash', str_repeat( 'a', 40 ) );
$request->set_param( 'file', 'index.html' );
$result = $this->proxy->serve_content( $request );
$this->assertInstanceOf( WP_Error::class, $result );
$this->assertEquals( 'file_not_found', $result->get_error_code() );
}
/**
* Test MIME types mapping includes common extensions.
*/
public function test_mime_types_mapping() {
$property = new ReflectionProperty( ExeLearning_Content_Proxy::class, 'mime_types' );
$property->setAccessible( true );
$mime_types = $property->getValue( $this->proxy );
$this->assertEquals( 'text/html', $mime_types['html'] );
$this->assertEquals( 'text/css', $mime_types['css'] );
$this->assertEquals( 'application/javascript', $mime_types['js'] );
$this->assertEquals( 'image/png', $mime_types['png'] );
$this->assertEquals( 'image/jpeg', $mime_types['jpg'] );
$this->assertEquals( 'application/pdf', $mime_types['pdf'] );
}
/**
* Test serve_content rejects directory traversal in file path.
*/
public function test_serve_content_rejects_traversal_in_file() {
$request = new WP_REST_Request( 'GET', '/exelearning/v1/content/test/file.html' );
$request->set_param( 'hash', str_repeat( 'a', 40 ) );
$request->set_param( 'file', '../../../etc/passwd' );
$result = $this->proxy->serve_content( $request );
$this->assertInstanceOf( WP_Error::class, $result );
$this->assertEquals( 'invalid_path', $result->get_error_code() );
}
/**
* Test serve_content defaults to index.html when file is empty.
*/
public function test_serve_content_defaults_to_index() {
$request = new WP_REST_Request( 'GET', '/exelearning/v1/content/test/' );
$request->set_param( 'hash', str_repeat( 'a', 40 ) );
$request->set_param( 'file', '' );
$result = $this->proxy->serve_content( $request );
// Should return file_not_found (not invalid_path) because it defaults to index.html.
$this->assertInstanceOf( WP_Error::class, $result );
$this->assertEquals( 'file_not_found', $result->get_error_code() );
}
/**
* Test sanitize_path handles current directory references.
*/
public function test_sanitize_path_handles_current_dir() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'sanitize_path' );
$method->setAccessible( true );
$result = $method->invoke( $this->proxy, './folder/./file.html' );
$this->assertEquals( 'folder/file.html', $result );
}
/**
* Test sanitize_path handles multiple consecutive slashes.
*/
public function test_sanitize_path_handles_multiple_slashes() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'sanitize_path' );
$method->setAccessible( true );
$result = $method->invoke( $this->proxy, 'folder//subfolder///file.html' );
$this->assertEquals( 'folder/subfolder/file.html', $result );
}
/**
* Test MIME types includes video formats.
*/
public function test_mime_types_includes_video() {
$property = new ReflectionProperty( ExeLearning_Content_Proxy::class, 'mime_types' );
$property->setAccessible( true );
$mime_types = $property->getValue( $this->proxy );
$this->assertEquals( 'video/mp4', $mime_types['mp4'] );
$this->assertEquals( 'video/webm', $mime_types['webm'] );
$this->assertEquals( 'video/ogg', $mime_types['ogv'] );
}
/**
* Test MIME types includes audio formats.
*/
public function test_mime_types_includes_audio() {
$property = new ReflectionProperty( ExeLearning_Content_Proxy::class, 'mime_types' );
$property->setAccessible( true );
$mime_types = $property->getValue( $this->proxy );
$this->assertEquals( 'audio/mpeg', $mime_types['mp3'] );
$this->assertEquals( 'audio/ogg', $mime_types['ogg'] );
$this->assertEquals( 'audio/wav', $mime_types['wav'] );
}
/**
* Test MIME types includes font formats.
*/
public function test_mime_types_includes_fonts() {
$property = new ReflectionProperty( ExeLearning_Content_Proxy::class, 'mime_types' );
$property->setAccessible( true );
$mime_types = $property->getValue( $this->proxy );
$this->assertEquals( 'font/woff', $mime_types['woff'] );
$this->assertEquals( 'font/woff2', $mime_types['woff2'] );
$this->assertEquals( 'font/ttf', $mime_types['ttf'] );
}
/**
* Test MIME types includes image formats.
*/
public function test_mime_types_includes_images() {
$property = new ReflectionProperty( ExeLearning_Content_Proxy::class, 'mime_types' );
$property->setAccessible( true );
$mime_types = $property->getValue( $this->proxy );
$this->assertEquals( 'image/gif', $mime_types['gif'] );
$this->assertEquals( 'image/svg+xml', $mime_types['svg'] );
$this->assertEquals( 'image/webp', $mime_types['webp'] );
$this->assertEquals( 'image/x-icon', $mime_types['ico'] );
}
/**
* Test MIME types includes document formats.
*/
public function test_mime_types_includes_documents() {
$property = new ReflectionProperty( ExeLearning_Content_Proxy::class, 'mime_types' );
$property->setAccessible( true );
$mime_types = $property->getValue( $this->proxy );
$this->assertEquals( 'application/json', $mime_types['json'] );
$this->assertEquals( 'application/xml', $mime_types['xml'] );
$this->assertEquals( 'application/zip', $mime_types['zip'] );
$this->assertEquals( 'text/plain', $mime_types['txt'] );
}
/**
* Test constructor sets base_path correctly.
*/
public function test_constructor_sets_base_path() {
$property = new ReflectionProperty( ExeLearning_Content_Proxy::class, 'base_path' );
$property->setAccessible( true );
$base_path = $property->getValue( $this->proxy );
$upload_dir = wp_upload_dir();
$expected = trailingslashit( $upload_dir['basedir'] ) . 'exelearning';
$this->assertEquals( $expected, $base_path );
}
/**
* Test get_proxy_url is static method.
*/
public function test_get_proxy_url_is_static() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'get_proxy_url' );
$this->assertTrue( $method->isStatic() );
}
/**
* Test serve_content is public.
*/
public function test_serve_content_is_public() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'serve_content' );
$this->assertTrue( $method->isPublic() );
}
/**
* Test send_headers method is private.
*/
public function test_send_headers_is_private() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'send_headers' );
$this->assertTrue( $method->isPrivate() );
}
/**
* Test sanitize_path method is private.
*/
public function test_sanitize_path_is_private() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'sanitize_path' );
$this->assertTrue( $method->isPrivate() );
}
/**
* Test serve_content with null hash.
*/
public function test_serve_content_with_null_hash() {
$request = new WP_REST_Request( 'GET', '/exelearning/v1/content/' );
$request->set_param( 'hash', null );
$result = $this->proxy->serve_content( $request );
$this->assertInstanceOf( WP_Error::class, $result );
$this->assertEquals( 'invalid_hash', $result->get_error_code() );
}
/**
* Test sanitize_path with complex traversal attempt.
*/
public function test_sanitize_path_complex_traversal() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'sanitize_path' );
$method->setAccessible( true );
$this->assertNull( $method->invoke( $this->proxy, 'valid/../../../etc/passwd' ) );
$this->assertNull( $method->invoke( $this->proxy, './..\\..\\windows' ) );
$this->assertNull( $method->invoke( $this->proxy, 'folder/..\\..\\secret' ) );
}
/**
* Test sanitize_path with valid nested path.
*/
public function test_sanitize_path_valid_nested() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'sanitize_path' );
$method->setAccessible( true );
$result = $method->invoke( $this->proxy, 'assets/css/style.css' );
$this->assertEquals( 'assets/css/style.css', $result );
}
/**
* Test sanitize_path with file in root.
*/
public function test_sanitize_path_root_file() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'sanitize_path' );
$method->setAccessible( true );
$result = $method->invoke( $this->proxy, 'index.html' );
$this->assertEquals( 'index.html', $result );
}
/**
* Test get_proxy_url returns correct format.
*/
public function test_get_proxy_url_format() {
$hash = str_repeat( 'c', 40 );
$url = ExeLearning_Content_Proxy::get_proxy_url( $hash, 'page1.html' );
$this->assertIsString( $url );
$this->assertStringContainsString( 'exelearning/v1/content', $url );
$this->assertStringContainsString( $hash, $url );
$this->assertStringContainsString( 'page1.html', $url );
}
/**
* Test sanitize_path handles leading slash.
*/
public function test_sanitize_path_handles_leading_slash() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'sanitize_path' );
$method->setAccessible( true );
$result = $method->invoke( $this->proxy, '/folder/file.html' );
$this->assertEquals( 'folder/file.html', $result );
}
/**
* Test sanitize_path handles trailing slash.
*/
public function test_sanitize_path_handles_trailing_slash() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'sanitize_path' );
$method->setAccessible( true );
$result = $method->invoke( $this->proxy, 'folder/' );
$this->assertEquals( 'folder', $result );
}
/**
* Test sanitize_path with double encoded path.
*/
public function test_sanitize_path_double_encoded() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'sanitize_path' );
$method->setAccessible( true );
// %252e%252e = double encoded ..
$result = $method->invoke( $this->proxy, '%252e%252e/secret' );
// After one decode: %2e%2e/secret
// This should not be decoded twice, so it's treated literally.
$this->assertNotNull( $result );
}
/**
* Test get_proxy_url with special characters in file.
*/
public function test_get_proxy_url_special_chars() {
$hash = str_repeat( 'd', 40 );
$url = ExeLearning_Content_Proxy::get_proxy_url( $hash, 'page 1.html' );
$this->assertStringContainsString( $hash, $url );
}
/**
* Test serve_content with uppercase hash.
*/
public function test_serve_content_uppercase_hash() {
$request = new WP_REST_Request( 'GET', '/exelearning/v1/content/test/index.html' );
$request->set_param( 'hash', strtoupper( str_repeat( 'a', 40 ) ) );
$request->set_param( 'file', 'index.html' );
$result = $this->proxy->serve_content( $request );
// Should accept uppercase hash (case insensitive).
$this->assertInstanceOf( WP_Error::class, $result );
$this->assertEquals( 'file_not_found', $result->get_error_code() );
}
/**
* Test serve_content with empty hash string.
*/
public function test_serve_content_empty_hash_string() {
$request = new WP_REST_Request( 'GET', '/exelearning/v1/content/' );
$request->set_param( 'hash', '' );
$result = $this->proxy->serve_content( $request );
$this->assertInstanceOf( WP_Error::class, $result );
$this->assertEquals( 'invalid_hash', $result->get_error_code() );
}
/**
* Test MIME types includes htm extension.
*/
public function test_mime_types_includes_htm() {
$property = new ReflectionProperty( ExeLearning_Content_Proxy::class, 'mime_types' );
$property->setAccessible( true );
$mime_types = $property->getValue( $this->proxy );
$this->assertEquals( 'text/html', $mime_types['htm'] );
}
/**
* Test MIME types includes jpeg extension.
*/
public function test_mime_types_includes_jpeg() {
$property = new ReflectionProperty( ExeLearning_Content_Proxy::class, 'mime_types' );
$property->setAccessible( true );
$mime_types = $property->getValue( $this->proxy );
$this->assertEquals( 'image/jpeg', $mime_types['jpeg'] );
}
/**
* Test MIME types includes eot font.
*/
public function test_mime_types_includes_eot() {
$property = new ReflectionProperty( ExeLearning_Content_Proxy::class, 'mime_types' );
$property->setAccessible( true );
$mime_types = $property->getValue( $this->proxy );
$this->assertEquals( 'application/vnd.ms-fontobject', $mime_types['eot'] );
}
/**
* Test MIME types includes otf font.
*/
public function test_mime_types_includes_otf() {
$property = new ReflectionProperty( ExeLearning_Content_Proxy::class, 'mime_types' );
$property->setAccessible( true );
$mime_types = $property->getValue( $this->proxy );
$this->assertEquals( 'font/otf', $mime_types['otf'] );
}
/**
* Test sanitize_path with spaces.
*/
public function test_sanitize_path_with_spaces() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'sanitize_path' );
$method->setAccessible( true );
$result = $method->invoke( $this->proxy, 'folder name/file name.html' );
$this->assertEquals( 'folder name/file name.html', $result );
}
/**
* Test sanitize_path with unicode.
*/
public function test_sanitize_path_with_unicode() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'sanitize_path' );
$method->setAccessible( true );
$result = $method->invoke( $this->proxy, 'carpeta/archivo.html' );
$this->assertEquals( 'carpeta/archivo.html', $result );
}
/**
* Test sanitize_path allows filenames starting with dots.
*/
public function test_sanitize_path_allows_dotted_filenames() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'sanitize_path' );
$method->setAccessible( true );
// ..valid is a filename, not a traversal attempt.
$result = $method->invoke( $this->proxy, 'folder/..valid/file' );
$this->assertEquals( 'folder/..valid/file', $result );
}
/**
* Test sanitize_path with only dots.
*/
public function test_sanitize_path_only_dots() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'sanitize_path' );
$method->setAccessible( true );
$result = $method->invoke( $this->proxy, '..' );
$this->assertNull( $result );
}
/**
* Test resolve_relative_path with empty base directory.
*/
public function test_resolve_relative_path_empty_base() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'resolve_relative_path' );
$method->setAccessible( true );
$result = $method->invoke( $this->proxy, '', 'file.html' );
$this->assertEquals( 'file.html', $result );
}
/**
* Test resolve_relative_path with parent directory traversal.
*/
public function test_resolve_relative_path_parent_traversal() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'resolve_relative_path' );
$method->setAccessible( true );
$result = $method->invoke( $this->proxy, 'html/css', '../images/logo.png' );
$this->assertEquals( 'html/images/logo.png', $result );
}
/**
* Test resolve_relative_path with multiple parent traversals.
*/
public function test_resolve_relative_path_multiple_traversals() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'resolve_relative_path' );
$method->setAccessible( true );
$result = $method->invoke( $this->proxy, 'a/b/c', '../../d/file.txt' );
$this->assertEquals( 'a/d/file.txt', $result );
}
/**
* Test resolve_relative_path with current directory references.
*/
public function test_resolve_relative_path_current_dir() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'resolve_relative_path' );
$method->setAccessible( true );
$result = $method->invoke( $this->proxy, 'folder', './file.html' );
$this->assertEquals( 'folder/file.html', $result );
}
/**
* Test resolve_relative_path with leading dot-slash in empty base.
*/
public function test_resolve_relative_path_empty_base_with_dotslash() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'resolve_relative_path' );
$method->setAccessible( true );
$result = $method->invoke( $this->proxy, '', './file.html' );
$this->assertEquals( 'file.html', $result );
}
/**
* Test resolve_relative_path with nested path.
*/
public function test_resolve_relative_path_nested() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'resolve_relative_path' );
$method->setAccessible( true );
$result = $method->invoke( $this->proxy, 'html', 'css/style.css' );
$this->assertEquals( 'html/css/style.css', $result );
}
/**
* Test rewrite_relative_urls with basic HTML.
*/
public function test_rewrite_relative_urls_basic() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'rewrite_relative_urls' );
$method->setAccessible( true );
$html = '<img src="images/logo.png">';
$hash = str_repeat( 'a', 40 );
$result = $method->invoke( $this->proxy, $html, $hash, '' );
$this->assertStringContainsString( 'uploads/exelearning/', $result );
$this->assertStringContainsString( 'images/logo.png', $result );
}
/**
* Test rewrite_relative_urls preserves absolute URLs.
*/
public function test_rewrite_relative_urls_preserves_absolute() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'rewrite_relative_urls' );
$method->setAccessible( true );
$html = '<img src="https://example.com/image.png">';
$hash = str_repeat( 'a', 40 );
$result = $method->invoke( $this->proxy, $html, $hash, '' );
$this->assertEquals( $html, $result );
}
/**
* Test rewrite_relative_urls preserves data URLs.
*/
public function test_rewrite_relative_urls_preserves_data_urls() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'rewrite_relative_urls' );
$method->setAccessible( true );
$html = '<img src="data:image/png;base64,ABC123">';
$hash = str_repeat( 'a', 40 );
$result = $method->invoke( $this->proxy, $html, $hash, '' );
$this->assertEquals( $html, $result );
}
/**
* Test rewrite_relative_urls handles href attributes.
*/
public function test_rewrite_relative_urls_handles_href() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'rewrite_relative_urls' );
$method->setAccessible( true );
$html = '<link href="css/style.css" rel="stylesheet">';
$hash = str_repeat( 'a', 40 );
$result = $method->invoke( $this->proxy, $html, $hash, '' );
$this->assertStringContainsString( 'uploads/exelearning/', $result );
$this->assertStringContainsString( 'css/style.css', $result );
}
/**
* Test rewrite_relative_urls handles poster attributes.
*/
public function test_rewrite_relative_urls_handles_poster() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'rewrite_relative_urls' );
$method->setAccessible( true );
$html = '<video poster="thumbnails/video.jpg"></video>';
$hash = str_repeat( 'a', 40 );
$result = $method->invoke( $this->proxy, $html, $hash, '' );
$this->assertStringContainsString( 'uploads/exelearning/', $result );
$this->assertStringContainsString( 'thumbnails/video.jpg', $result );
}
/**
* Test rewrite_relative_urls handles inline style url().
*/
public function test_rewrite_relative_urls_handles_inline_style() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'rewrite_relative_urls' );
$method->setAccessible( true );
$html = '<div style="background: url(images/bg.png)"></div>';
$hash = str_repeat( 'a', 40 );
$result = $method->invoke( $this->proxy, $html, $hash, '' );
$this->assertStringContainsString( 'uploads/exelearning/', $result );
$this->assertStringContainsString( 'images/bg.png', $result );
}
/**
* Test rewrite_relative_urls with file in subdirectory.
*/
public function test_rewrite_relative_urls_from_subdirectory() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'rewrite_relative_urls' );
$method->setAccessible( true );
$html = '<img src="../images/logo.png">';
$hash = str_repeat( 'a', 40 );
$result = $method->invoke( $this->proxy, $html, $hash, 'html/page.html' );
$this->assertStringContainsString( 'images/logo.png', $result );
}
/**
* Test rewrite_relative_urls preserves javascript: links.
*/
public function test_rewrite_relative_urls_preserves_javascript() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'rewrite_relative_urls' );
$method->setAccessible( true );
$html = '<a href="javascript:void(0)">Click</a>';
$hash = str_repeat( 'a', 40 );
$result = $method->invoke( $this->proxy, $html, $hash, '' );
$this->assertEquals( $html, $result );
}
/**
* Test rewrite_relative_urls preserves hash links.
*/
public function test_rewrite_relative_urls_preserves_hash() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'rewrite_relative_urls' );
$method->setAccessible( true );
$html = '<a href="#section">Section</a>';
$hash = str_repeat( 'a', 40 );
$result = $method->invoke( $this->proxy, $html, $hash, '' );
$this->assertEquals( $html, $result );
}
/**
* Test rewrite_relative_urls preserves protocol-relative URLs.
*/
public function test_rewrite_relative_urls_preserves_protocol_relative() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'rewrite_relative_urls' );
$method->setAccessible( true );
$html = '<img src="//cdn.example.com/image.png">';
$hash = str_repeat( 'a', 40 );
$result = $method->invoke( $this->proxy, $html, $hash, '' );
$this->assertEquals( $html, $result );
}
/**
* Test rewrite_relative_urls handles multiple attributes.
*/
public function test_rewrite_relative_urls_multiple_elements() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'rewrite_relative_urls' );
$method->setAccessible( true );
$html = '<img src="a.png"><img src="b.png"><link href="c.css">';
$hash = str_repeat( 'a', 40 );
$result = $method->invoke( $this->proxy, $html, $hash, '' );
$this->assertStringContainsString( 'a.png', $result );
$this->assertStringContainsString( 'b.png', $result );
$this->assertStringContainsString( 'c.css', $result );
}
/**
* Test resolve_relative_path handles traversal beyond root.
*/
public function test_resolve_relative_path_beyond_root() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'resolve_relative_path' );
$method->setAccessible( true );
// Going up more levels than available should result in empty or valid path.
$result = $method->invoke( $this->proxy, 'a', '../../file.txt' );
$this->assertEquals( 'file.txt', $result );
}
/**
* Test rewrite_relative_urls with empty src.
*/
public function test_rewrite_relative_urls_empty_src() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'rewrite_relative_urls' );
$method->setAccessible( true );
$html = '<img src="">';
$hash = str_repeat( 'a', 40 );
$result = $method->invoke( $this->proxy, $html, $hash, '' );
$this->assertEquals( $html, $result );
}
/**
* Test rewrite_relative_urls with absolute path.
*/
public function test_rewrite_relative_urls_absolute_path() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'rewrite_relative_urls' );
$method->setAccessible( true );
$html = '<img src="/images/logo.png">';
$hash = str_repeat( 'a', 40 );
$result = $method->invoke( $this->proxy, $html, $hash, '' );
// Absolute paths starting with / should not be rewritten.
$this->assertEquals( $html, $result );
}
/**
* Test rewrite_relative_urls routes HTML links through proxy.
*/
public function test_rewrite_relative_urls_html_links_use_proxy() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'rewrite_relative_urls' );
$method->setAccessible( true );
$html = '<a href="html/page2.html">Next</a>';
$hash = str_repeat( 'a', 40 );
$result = $method->invoke( $this->proxy, $html, $hash, '' );
$this->assertStringContainsString( 'exelearning/v1/content/', $result );
$this->assertStringContainsString( 'html/page2.html', $result );
$this->assertStringNotContainsString( 'uploads/exelearning/', $result );
}
/**
* Test rewrite_relative_urls routes HTM links through proxy.
*/
public function test_rewrite_relative_urls_htm_links_use_proxy() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'rewrite_relative_urls' );
$method->setAccessible( true );
$html = '<a href="page.htm">Page</a>';
$hash = str_repeat( 'a', 40 );
$result = $method->invoke( $this->proxy, $html, $hash, '' );
$this->assertStringContainsString( 'exelearning/v1/content/', $result );
$this->assertStringNotContainsString( 'uploads/exelearning/', $result );
}
/**
* Test validate_hash with valid hash.
*/
public function test_validate_hash_valid() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'validate_hash' );
$method->setAccessible( true );
$result = $method->invoke( $this->proxy, str_repeat( 'a', 40 ) );
$this->assertTrue( $result );
}
/**
* Test validate_hash with invalid hash.
*/
public function test_validate_hash_invalid() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'validate_hash' );
$method->setAccessible( true );
$result = $method->invoke( $this->proxy, 'invalid' );
$this->assertInstanceOf( WP_Error::class, $result );
$this->assertEquals( 'invalid_hash', $result->get_error_code() );
}
/**
* Test validate_hash with null.
*/
public function test_validate_hash_null() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'validate_hash' );
$method->setAccessible( true );
$result = $method->invoke( $this->proxy, null );
$this->assertInstanceOf( WP_Error::class, $result );
}
/**
* Test validate_hash with empty string.
*/
public function test_validate_hash_empty() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'validate_hash' );
$method->setAccessible( true );
$result = $method->invoke( $this->proxy, '' );
$this->assertInstanceOf( WP_Error::class, $result );
}
/**
* Test validate_file_path with empty file defaults to index.html.
*/
public function test_validate_file_path_empty_defaults() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'validate_file_path' );
$method->setAccessible( true );
// Will return file_not_found because directory doesn't exist,
// but we can verify it tried with index.html by the error.
$result = $method->invoke( $this->proxy, '', str_repeat( 'a', 40 ) );
$this->assertInstanceOf( WP_Error::class, $result );
$this->assertEquals( 'file_not_found', $result->get_error_code() );
}
/**
* Test validate_file_path with traversal attempt.
*/
public function test_validate_file_path_traversal() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'validate_file_path' );
$method->setAccessible( true );
$result = $method->invoke( $this->proxy, '../../../etc/passwd', str_repeat( 'a', 40 ) );
$this->assertInstanceOf( WP_Error::class, $result );
$this->assertEquals( 'invalid_path', $result->get_error_code() );
}
/**
* Test validate_file_path with valid but non-existent file.
*/
public function test_validate_file_path_not_found() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'validate_file_path' );
$method->setAccessible( true );
$result = $method->invoke( $this->proxy, 'nonexistent.html', str_repeat( 'a', 40 ) );
$this->assertInstanceOf( WP_Error::class, $result );
$this->assertEquals( 'file_not_found', $result->get_error_code() );
}
/**
* Test validate_hash is private method.
*/
public function test_validate_hash_is_private() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'validate_hash' );
$this->assertTrue( $method->isPrivate() );
}
/**
* Test validate_file_path is private method.
*/
public function test_validate_file_path_is_private() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'validate_file_path' );
$this->assertTrue( $method->isPrivate() );
}
/**
* Test serve_file is private method.
*/
public function test_serve_file_is_private() {
$method = new ReflectionMethod( ExeLearning_Content_Proxy::class, 'serve_file' );
$this->assertTrue( $method->isPrivate() );
}
}