loadScriptTextdomain.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * @group l10n
  4. * @group i18n
  5. */
  6. class Tests_L10n_LoadScriptTextdomain extends WP_UnitTestCase {
  7. /**
  8. * @ticket 45528
  9. * @ticket 46336
  10. * @ticket 46387
  11. * @ticket 49145
  12. *
  13. * @dataProvider data_test_resolve_relative_path
  14. */
  15. public function test_resolve_relative_path( $translation_path, $handle, $src, $textdomain, $filter = array() ) {
  16. if ( ! empty( $filter ) ) {
  17. add_filter( $filter[0], $filter[1], 10, isset( $filter[2] ) ? $filter[2] : 1 );
  18. }
  19. wp_enqueue_script( $handle, $src, array(), null );
  20. $expected = file_get_contents( DIR_TESTDATA . $translation_path );
  21. $this->assertSame( $expected, load_script_textdomain( $handle, $textdomain, DIR_TESTDATA . '/languages' ) );
  22. }
  23. public function data_test_resolve_relative_path() {
  24. return array(
  25. // @ticket 45528
  26. array(
  27. '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json',
  28. 'test-example-root',
  29. '/wp-includes/js/script.js',
  30. 'default',
  31. ),
  32. // Assets on a CDN.
  33. array(
  34. '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json',
  35. 'test-example-cdn',
  36. 'https://my-cdn.com/wordpress/wp-includes/js/script.js',
  37. 'default',
  38. array( 'load_script_textdomain_relative_path', array( $this, 'relative_path_from_cdn' ), 2 ),
  39. ),
  40. // Test for WordPress installs in a subdirectory.
  41. array(
  42. '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json',
  43. 'test-example-subdir',
  44. '/wp/wp-includes/js/script.js',
  45. 'default',
  46. array(
  47. 'site_url',
  48. function ( $site_url ) {
  49. return $site_url . '/wp';
  50. },
  51. ),
  52. ),
  53. // @ticket 46336
  54. array(
  55. '/languages/plugins/internationalized-plugin-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json',
  56. 'plugin-example-1',
  57. 'https://plugins.example.com/my-plugin/js/script.js',
  58. 'internationalized-plugin',
  59. array(
  60. 'plugins_url',
  61. function () {
  62. return 'https://plugins.example.com';
  63. },
  64. ),
  65. ),
  66. // @ticket 46387
  67. array(
  68. '/languages/plugins/internationalized-plugin-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json',
  69. 'plugin-example-2',
  70. 'https://content.example.com/plugins/my-plugin/js/script.js',
  71. 'internationalized-plugin',
  72. array(
  73. 'content_url',
  74. function () {
  75. return 'https://content.example.com';
  76. },
  77. ),
  78. ),
  79. // @ticket 49145
  80. array(
  81. '/languages/plugins/internationalized-plugin-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json',
  82. 'test-when-no-content_url-host',
  83. 'https://content.example.com/plugins/my-plugin/js/script.js',
  84. 'internationalized-plugin',
  85. array(
  86. 'content_url',
  87. function () {
  88. return '/';
  89. },
  90. ),
  91. ),
  92. // @ticket 49145
  93. array(
  94. '/languages/plugins/internationalized-plugin-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json',
  95. 'test-when-no-plugins_url-host',
  96. 'https://plugins.example.com/my-plugin/js/script.js',
  97. 'internationalized-plugin',
  98. array(
  99. 'plugins_url',
  100. function () {
  101. return '/';
  102. },
  103. ),
  104. ),
  105. // @ticket 49145
  106. array(
  107. '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json',
  108. 'test-when-no-site_url-host',
  109. '/wp/wp-includes/js/script.js',
  110. 'default',
  111. array(
  112. 'site_url',
  113. function () {
  114. return '/wp';
  115. },
  116. ),
  117. ),
  118. );
  119. }
  120. public function relative_path_from_cdn( $relative, $src ) {
  121. if ( 0 === strpos( $src, 'https://my-cdn.com/wordpress/' ) ) {
  122. return substr( $src, strlen( 'https://my-cdn.com/wordpress/' ) );
  123. }
  124. return $relative;
  125. }
  126. }