themeFile.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * @group link
  4. */
  5. class Tests_Link_ThemeFile extends WP_UnitTestCase {
  6. public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
  7. $themes = array(
  8. 'theme-file-parent',
  9. 'theme-file-child',
  10. );
  11. // Copy themes from tests/phpunit/data to wp-content/themes.
  12. foreach ( $themes as $theme ) {
  13. $source_dir = DIR_TESTDATA . '/' . $theme;
  14. $dest_dir = WP_CONTENT_DIR . '/themes/' . $theme;
  15. mkdir( $dest_dir );
  16. foreach ( glob( $source_dir . '/*.*' ) as $theme_file ) {
  17. copy( $theme_file, $dest_dir . '/' . basename( $theme_file ) );
  18. }
  19. }
  20. }
  21. public static function wpTearDownAfterClass() {
  22. $themes = array(
  23. 'theme-file-parent',
  24. 'theme-file-child',
  25. );
  26. // Remove previously copied themes from wp-content/themes.
  27. foreach ( $themes as $theme ) {
  28. $dest_dir = WP_CONTENT_DIR . '/themes/' . $theme;
  29. foreach ( glob( $dest_dir . '/*.*' ) as $theme_file ) {
  30. unlink( $theme_file );
  31. }
  32. rmdir( $dest_dir );
  33. }
  34. }
  35. /**
  36. * @ticket 18302
  37. *
  38. * @dataProvider data_theme_files
  39. *
  40. * @covers ::get_theme_file_uri
  41. * @covers ::get_parent_theme_file_uri
  42. */
  43. public function test_theme_file_uri_with_parent_theme( $file, $expected_theme, $existence ) {
  44. switch_theme( 'theme-file-parent' );
  45. // Ensure the returned URL always uses the parent theme:
  46. $this->assertSame( content_url( "themes/theme-file-parent/{$file}" ), get_theme_file_uri( $file ) );
  47. $this->assertSame( content_url( "themes/theme-file-parent/{$file}" ), get_parent_theme_file_uri( $file ) );
  48. }
  49. /**
  50. * @ticket 18302
  51. *
  52. * @dataProvider data_theme_files
  53. *
  54. * @covers ::get_theme_file_uri
  55. * @covers ::get_parent_theme_file_uri
  56. */
  57. public function test_theme_file_uri_with_child_theme( $file, $expected_theme, $existence ) {
  58. switch_theme( 'theme-file-child' );
  59. // Ensure the returned URL uses the expected theme:
  60. $this->assertSame( content_url( "themes/{$expected_theme}/{$file}" ), get_theme_file_uri( $file ) );
  61. // Ensure the returned URL always uses the parent theme:
  62. $this->assertSame( content_url( "themes/theme-file-parent/{$file}" ), get_parent_theme_file_uri( $file ) );
  63. }
  64. /**
  65. * @ticket 18302
  66. *
  67. * @dataProvider data_theme_files
  68. *
  69. * @covers ::get_theme_file_path
  70. * @covers ::get_parent_theme_file_path
  71. */
  72. public function test_theme_file_path_with_parent_theme( $file, $expected_theme, $existence ) {
  73. switch_theme( 'theme-file-parent' );
  74. // Ensure the returned path always uses the parent theme:
  75. $this->assertSame( WP_CONTENT_DIR . "/themes/theme-file-parent/{$file}", get_theme_file_path( $file ) );
  76. $this->assertSame( WP_CONTENT_DIR . "/themes/theme-file-parent/{$file}", get_parent_theme_file_path( $file ) );
  77. }
  78. /**
  79. * @ticket 18302
  80. *
  81. * @dataProvider data_theme_files
  82. *
  83. * @covers ::get_theme_file_path
  84. * @covers ::get_parent_theme_file_path
  85. */
  86. public function test_theme_file_path_with_child_theme( $file, $expected_theme, $existence ) {
  87. switch_theme( 'theme-file-child' );
  88. // Ensure the returned path uses the expected theme:
  89. $this->assertSame( WP_CONTENT_DIR . "/themes/{$expected_theme}/{$file}", get_theme_file_path( $file ) );
  90. // Ensure the returned path always uses the parent theme:
  91. $this->assertSame( WP_CONTENT_DIR . "/themes/theme-file-parent/{$file}", get_parent_theme_file_path( $file ) );
  92. }
  93. /**
  94. * Test the tests.
  95. *
  96. * @ticket 18302
  97. *
  98. * @dataProvider data_theme_files
  99. */
  100. public function test_theme_file_existance( $file, $expected_theme, $existence ) {
  101. if ( in_array( 'theme-file-child', $existence, true ) ) {
  102. $this->assertFileExists( WP_CONTENT_DIR . "/themes/theme-file-child/{$file}" );
  103. } else {
  104. $this->assertFileNotExists( WP_CONTENT_DIR . "/themes/theme-file-child/{$file}" );
  105. }
  106. if ( in_array( 'theme-file-parent', $existence, true ) ) {
  107. $this->assertFileExists( WP_CONTENT_DIR . "/themes/theme-file-parent/{$file}" );
  108. } else {
  109. $this->assertFileNotExists( WP_CONTENT_DIR . "/themes/theme-file-parent/{$file}" );
  110. }
  111. }
  112. /**
  113. * @ticket 18302
  114. *
  115. * @dataProvider data_theme_files
  116. *
  117. * @covers ::get_theme_file_uri
  118. * @covers ::get_parent_theme_file_uri
  119. */
  120. public function test_theme_file_uri_returns_valid_uri( $file, $expected_theme, $existence ) {
  121. $uri = get_theme_file_uri( $file );
  122. $parent_uri = get_parent_theme_file_uri( $file );
  123. $this->assertSame( esc_url_raw( $uri ), $uri );
  124. $this->assertSame( esc_url_raw( $parent_uri ), $parent_uri );
  125. }
  126. public function data_theme_files() {
  127. $parent = 'theme-file-parent';
  128. $child = 'theme-file-child';
  129. return array(
  130. array(
  131. 'parent-only.php',
  132. $parent,
  133. array(
  134. $parent,
  135. ),
  136. ),
  137. array(
  138. 'child-only.php',
  139. $child,
  140. array(
  141. $child,
  142. ),
  143. ),
  144. array(
  145. 'parent-and-child.php',
  146. $child,
  147. array(
  148. $parent,
  149. $child,
  150. ),
  151. ),
  152. array(
  153. 'neither.php',
  154. $parent,
  155. array(),
  156. ),
  157. );
  158. }
  159. }