CommonTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php declare( strict_types=1 );
  2. use PHPUnit\Framework\TestCase;
  3. // For suppressing "Undefined index" notices
  4. $_SERVER['REQUEST_SCHEME'] = '';
  5. $_SERVER['HTTP_HOST'] = '';
  6. $_SERVER['SCRIPT_NAME'] = '';
  7. // We would have to include the file manually since composer won't be
  8. // able to import it because it's not class.
  9. require_once( dirname( __DIR__ ) . '/inc/common.php' );
  10. final class CommonTest extends TestCase
  11. {
  12. private $test_base_url;
  13. protected function setUp(): void
  14. {
  15. // For testing code where $_SERVER variables are needed
  16. $_SERVER['REQUEST_SCHEME'] = 'http';
  17. $_SERVER['HTTP_HOST'] = 'localhost';
  18. $_SERVER['SCRIPT_NAME'] = '/vioscope/index.php';
  19. // Stores test base_url
  20. $this->test_base_url = 'http://localhost/vioscope';
  21. parent::setUp();
  22. }
  23. public function test_get_base_url(): void
  24. {
  25. // Unset config to test auto-detect base_url (through $_SERVER)
  26. unset( $GLOBALS['config']['base_url'] );
  27. $this->assertEquals(
  28. $this->test_base_url,
  29. get_base_url()
  30. );
  31. // Test if the $config value for base_url is respected
  32. $GLOBALS['config']['base_url'] = 'https://example.com';
  33. $this->assertEquals(
  34. 'https://example.com',
  35. get_base_url()
  36. );
  37. // Unset for later tests
  38. unset( $GLOBALS['config']['base_url'] );
  39. }
  40. public function test_get_local_url(): void
  41. {
  42. // Test local video url generation
  43. $this->assertEquals(
  44. 'http://localhost/vioscope/watch?v=C0DPdy98e4c',
  45. get_local_url( 'C0DPdy98e4c', 'video' )
  46. );
  47. // Test local channel url generation
  48. $this->assertEquals(
  49. 'http://localhost/vioscope/channel/UCHDm-DKoMyJxKVgwGmuTaQA',
  50. get_local_url( 'UCHDm-DKoMyJxKVgwGmuTaQA', 'channel' )
  51. );
  52. }
  53. public function test_replace_urls_with_local_urls(): void
  54. {
  55. $this->assertEquals(
  56. $this->test_base_url . '/watch?v=C0DPdy98e4c&feature=feedrec_grec_index',
  57. replace_urls_with_local_urls( 'https://www.youtube.com/watch?v=C0DPdy98e4c&feature=feedrec_grec_index' )
  58. );
  59. $this->assertEquals(
  60. $this->test_base_url . '/watch?v=C0DPdy98e4c',
  61. replace_urls_with_local_urls( 'http://youtu.be/C0DPdy98e4c' )
  62. );
  63. $this->assertEquals(
  64. $this->test_base_url . '/watch?v=C0DPdy98e4c?rel=0',
  65. replace_urls_with_local_urls( 'https://www.youtube.com/embed/C0DPdy98e4c?rel=0' )
  66. );
  67. $this->assertEquals(
  68. $this->test_base_url . '/user/SomeUser_Name#p/a/u/1/QdK8U-VIH_o',
  69. replace_urls_with_local_urls( 'https://www.youtube.com/user/SomeUser_Name#p/a/u/1/QdK8U-VIH_o' )
  70. );
  71. $this->assertEquals(
  72. $this->test_base_url . '/channel/UCHDm-DKoMyJxKVgwGmuTaQA',
  73. replace_urls_with_local_urls( 'https://youtube.com/channel/UCHDm-DKoMyJxKVgwGmuTaQA' )
  74. );
  75. }
  76. public function test_urlify_string(): void
  77. {
  78. $this->assertEquals(
  79. 'Example <a href="https://www.youtube.com/watch?v=C0DPdy98e4c">https://www.youtube.com/watch?v=C0DPdy98e4c</a> text',
  80. urlify_string( 'Example https://www.youtube.com/watch?v=C0DPdy98e4c text' )
  81. );
  82. $this->assertEquals(
  83. 'Example <a href="http://192.168.0.2/watch?v=C0DPdy98e4c">http://192.168.0.2/watch?v=C0DPdy98e4c</a>',
  84. urlify_string( 'Example http://192.168.0.2/watch?v=C0DPdy98e4c' )
  85. );
  86. }
  87. public function test_get_video_thumbnail_url(): void
  88. {
  89. $this->assertEquals(
  90. 'https://i.ytimg.com/vi/C0DPdy98e4c/mqdefault.jpg',
  91. get_video_thumbnail_url( 'C0DPdy98e4c' )
  92. );
  93. $this->assertEquals(
  94. 'https://i.ytimg.com/vi/C0DPdy98e4c/default.jpg',
  95. get_video_thumbnail_url( 'C0DPdy98e4c', 'small' )
  96. );
  97. $this->assertEquals(
  98. 'https://i.ytimg.com/vi/C0DPdy98e4c/hqdefault.jpg',
  99. get_video_thumbnail_url( 'C0DPdy98e4c', 'high' )
  100. );
  101. }
  102. }