PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/phpunit/tests/admin/includesPlugin.php

https://gitlab.com/morganestes/wordpress-develop
PHP | 494 lines | 306 code | 74 blank | 114 comment | 34 complexity | c43fdefc14238eb33f2cfa24b0953d32 MD5 | raw file
  1. <?php
  2. /**
  3. * @group plugins
  4. * @group admin
  5. */
  6. class Tests_Admin_includesPlugin extends WP_UnitTestCase {
  7. function test_get_plugin_data() {
  8. $data = get_plugin_data( DIR_TESTDATA . '/plugins/hello.php' );
  9. $default_headers = array(
  10. 'Name' => 'Hello Dolly',
  11. 'Title' => '<a href="http://wordpress.org/#">Hello Dolly</a>',
  12. 'PluginURI' => 'http://wordpress.org/#',
  13. 'Description' => 'This is not just a plugin, it symbolizes the hope and enthusiasm of an entire generation summed up in two words sung most famously by Louis Armstrong: Hello, Dolly. When activated you will randomly see a lyric from Hello, Dolly in the upper right of your admin screen on every page. <cite>By <a href="http://ma.tt/">Matt Mullenweg</a>.</cite>',
  14. 'Author' => '<a href="http://ma.tt/">Matt Mullenweg</a>',
  15. 'AuthorURI' => 'http://ma.tt/',
  16. 'Version' => '1.5.1',
  17. 'TextDomain' => 'hello-dolly',
  18. 'DomainPath' => '',
  19. );
  20. $this->assertTrue( is_array( $data ) );
  21. foreach ( $default_headers as $name => $value ) {
  22. $this->assertTrue( isset( $data[ $name ] ) );
  23. $this->assertEquals( $value, $data[ $name ] );
  24. }
  25. }
  26. function test_menu_page_url() {
  27. $current_user = get_current_user_id();
  28. wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
  29. update_option( 'siteurl', 'http://example.com' );
  30. // add some pages
  31. add_options_page( 'Test Settings', 'Test Settings', 'manage_options', 'testsettings', 'mt_settings_page' );
  32. add_management_page( 'Test Tools', 'Test Tools', 'manage_options', 'testtools', 'mt_tools_page' );
  33. add_menu_page( 'Test Toplevel', 'Test Toplevel', 'manage_options', 'mt-top-level-handle', 'mt_toplevel_page' );
  34. add_submenu_page( 'mt-top-level-handle', 'Test Sublevel', 'Test Sublevel', 'manage_options', 'sub-page', 'mt_sublevel_page' );
  35. add_submenu_page( 'mt-top-level-handle', 'Test Sublevel 2', 'Test Sublevel 2', 'manage_options', 'sub-page2', 'mt_sublevel_page2' );
  36. add_theme_page( 'With Spaces', 'With Spaces', 'manage_options', 'With Spaces', 'mt_tools_page' );
  37. add_pages_page( 'Appending Query Arg', 'Test Pages', 'edit_pages', 'testpages', 'mt_pages_page' );
  38. $expected['testsettings'] = 'http://example.com/wp-admin/options-general.php?page=testsettings';
  39. $expected['testtools'] = 'http://example.com/wp-admin/tools.php?page=testtools';
  40. $expected['mt-top-level-handle'] = 'http://example.com/wp-admin/admin.php?page=mt-top-level-handle';
  41. $expected['sub-page'] = 'http://example.com/wp-admin/admin.php?page=sub-page';
  42. $expected['sub-page2'] = 'http://example.com/wp-admin/admin.php?page=sub-page2';
  43. $expected['not_registered'] = '';
  44. $expected['With Spaces'] = 'http://example.com/wp-admin/themes.php?page=With%20Spaces';
  45. $expected['testpages'] = 'http://example.com/wp-admin/edit.php?post_type=page&#038;page=testpages';
  46. foreach ( $expected as $name => $value ) {
  47. $this->assertEquals( $value, menu_page_url( $name, false ) );
  48. }
  49. wp_set_current_user( $current_user );
  50. }
  51. function test_is_plugin_active_true() {
  52. activate_plugin( 'hello.php' );
  53. $test = is_plugin_active( 'hello.php' );
  54. $this->assertTrue( $test );
  55. deactivate_plugins( 'hello.php' );
  56. }
  57. function test_is_plugin_active_false() {
  58. deactivate_plugins( 'hello.php' );
  59. $test = is_plugin_active( 'hello.php' );
  60. $this->assertFalse( $test );
  61. }
  62. function test_is_plugin_inactive_true() {
  63. deactivate_plugins( 'hello.php' );
  64. $test = is_plugin_inactive( 'hello.php' );
  65. $this->assertTrue( $test );
  66. }
  67. function test_is_plugin_inactive_false() {
  68. activate_plugin( 'hello.php' );
  69. $test = is_plugin_inactive( 'hello.php' );
  70. $this->assertFalse( $test );
  71. deactivate_plugins( 'hello.php' );
  72. }
  73. /**
  74. * @covers ::get_plugin_files
  75. */
  76. public function test_get_plugin_files_single() {
  77. $name = 'hello.php';
  78. $this->assertEquals( array( $name ), get_plugin_files( $name ) );
  79. }
  80. /**
  81. * @covers ::get_plugin_files
  82. */
  83. public function test_get_plugin_files_folder() {
  84. $plugin_dir = WP_PLUGIN_DIR . '/list_files_test_plugin';
  85. @mkdir( $plugin_dir );
  86. $plugin = $this->_create_plugin( null, 'list_files_test_plugin.php', $plugin_dir );
  87. $sub_dir = trailingslashit( dirname( $plugin[1] ) ) . 'subdir';
  88. @mkdir( $sub_dir );
  89. @file_put_contents( $sub_dir . '/subfile.php', '<?php // Silence.' );
  90. $plugin_files = get_plugin_files( plugin_basename( $plugin[1] ) );
  91. $expected = array(
  92. 'list_files_test_plugin/list_files_test_plugin.php',
  93. 'list_files_test_plugin/subdir/subfile.php',
  94. );
  95. $this->assertEquals( $expected, $plugin_files );
  96. unlink( $sub_dir . '/subfile.php' );
  97. unlink( $plugin[1] );
  98. rmdir( $sub_dir );
  99. rmdir( $plugin_dir );
  100. }
  101. /**
  102. * @covers ::get_mu_plugins
  103. */
  104. public function test_get_mu_plugins_when_mu_plugins_exists_but_is_empty() {
  105. if ( is_dir( WPMU_PLUGIN_DIR ) ) {
  106. $exists = true;
  107. $this->_back_up_mu_plugins();
  108. } else {
  109. $exists = false;
  110. mkdir( WPMU_PLUGIN_DIR );
  111. }
  112. $this->assertEquals( array(), get_mu_plugins() );
  113. // Clean up.
  114. if ( $exists ) {
  115. $this->_restore_mu_plugins();
  116. } else {
  117. rmdir( WPMU_PLUGIN_DIR );
  118. }
  119. }
  120. /**
  121. * @covers ::get_mu_plugins
  122. */
  123. public function test_get_mu_plugins_when_mu_plugins_directory_does_not_exist() {
  124. $exists = false;
  125. if ( is_dir( WPMU_PLUGIN_DIR ) ) {
  126. $exists = true;
  127. $this->_back_up_mu_plugins();
  128. rmdir( WPMU_PLUGIN_DIR );
  129. }
  130. $this->assertEquals( array(), get_mu_plugins() );
  131. // Clean up.
  132. if ( $exists ) {
  133. mkdir( WPMU_PLUGIN_DIR );
  134. $this->_restore_mu_plugins();
  135. }
  136. }
  137. /**
  138. * @covers ::get_mu_plugins
  139. */
  140. public function test_get_mu_plugins_should_ignore_index_php_containing_silence_is_golden() {
  141. if ( is_dir( WPMU_PLUGIN_DIR ) ) {
  142. $exists = true;
  143. $this->_back_up_mu_plugins();
  144. } else {
  145. $exists = false;
  146. mkdir( WPMU_PLUGIN_DIR );
  147. }
  148. $this->_create_plugin( '<?php\n//Silence is golden.', 'index.php', WPMU_PLUGIN_DIR );
  149. $this->assertEquals( array(), get_mu_plugins() );
  150. // Clean up.
  151. unlink( WPMU_PLUGIN_DIR . '/index.php' );
  152. if ( $exists ) {
  153. $this->_restore_mu_plugins();
  154. } else {
  155. rmdir( WPMU_PLUGIN_DIR );
  156. }
  157. }
  158. /**
  159. * @covers ::get_mu_plugins
  160. */
  161. public function test_get_mu_plugins_should_not_ignore_index_php_containing_something_other_than_silence_is_golden() {
  162. if ( is_dir( WPMU_PLUGIN_DIR ) ) {
  163. $exists = true;
  164. $this->_back_up_mu_plugins();
  165. } else {
  166. $exists = false;
  167. mkdir( WPMU_PLUGIN_DIR );
  168. }
  169. $this->_create_plugin( '<?php\n//Silence is not golden.', 'index.php', WPMU_PLUGIN_DIR );
  170. $found = get_mu_plugins();
  171. $this->assertEquals( array( 'index.php' ), array_keys( $found ) );
  172. // Clean up.
  173. unlink( WPMU_PLUGIN_DIR . '/index.php' );
  174. if ( $exists ) {
  175. $this->_restore_mu_plugins();
  176. } else {
  177. rmdir( WPMU_PLUGIN_DIR );
  178. }
  179. }
  180. /**
  181. * @covers ::get_mu_plugins
  182. */
  183. public function test_get_mu_plugins_should_ignore_files_without_php_extensions() {
  184. if ( is_dir( WPMU_PLUGIN_DIR ) ) {
  185. $exists = true;
  186. $this->_back_up_mu_plugins();
  187. } else {
  188. $exists = false;
  189. mkdir( WPMU_PLUGIN_DIR );
  190. }
  191. $this->_create_plugin( '<?php\n//Test', 'foo.php', WPMU_PLUGIN_DIR );
  192. $this->_create_plugin( '<?php\n//Test 2', 'bar.txt', WPMU_PLUGIN_DIR );
  193. $found = get_mu_plugins();
  194. $this->assertEquals( array( 'foo.php' ), array_keys( $found ) );
  195. // Clean up.
  196. unlink( WPMU_PLUGIN_DIR . '/foo.php' );
  197. unlink( WPMU_PLUGIN_DIR . '/bar.txt' );
  198. if ( $exists ) {
  199. $this->_restore_mu_plugins();
  200. } else {
  201. rmdir( WPMU_PLUGIN_DIR );
  202. }
  203. }
  204. /**
  205. * @covers ::_sort_uname_callback
  206. */
  207. public function test__sort_uname_callback() {
  208. $this->assertLessThan( 0, _sort_uname_callback( array( 'Name' => 'a' ), array( 'Name' => 'b' ) ) );
  209. $this->assertGreaterThan( 0, _sort_uname_callback( array( 'Name' => 'c' ), array( 'Name' => 'b' ) ) );
  210. $this->assertEquals( 0, _sort_uname_callback( array( 'Name' => 'a' ), array( 'Name' => 'a' ) ) );
  211. }
  212. /**
  213. * @covers ::get_dropins
  214. */
  215. public function test_get_dropins_empty() {
  216. $this->_back_up_drop_ins();
  217. $this->assertEquals( array(), get_dropins() );
  218. // Clean up.
  219. $this->_restore_drop_ins();
  220. }
  221. /**
  222. * @covers ::get_dropins
  223. */
  224. public function test_get_dropins_not_empty() {
  225. $this->_back_up_drop_ins();
  226. $p1 = $this->_create_plugin( "<?php\n//Test", 'advanced-cache.php', WP_CONTENT_DIR );
  227. $p2 = $this->_create_plugin( "<?php\n//Test", 'not-a-dropin.php', WP_CONTENT_DIR );
  228. $dropins = get_dropins();
  229. $this->assertEquals( array( 'advanced-cache.php' ), array_keys( $dropins ) );
  230. unlink( $p1[1] );
  231. unlink( $p2[1] );
  232. // Clean up.
  233. $this->_restore_drop_ins();
  234. }
  235. /**
  236. * @covers ::is_network_only_plugin
  237. */
  238. public function test_is_network_only_plugin_hello() {
  239. $this->assertFalse( is_network_only_plugin( 'hello.php' ) );
  240. }
  241. /**
  242. * @covers ::is_network_only_plugin
  243. */
  244. public function test_is_network_only_plugin() {
  245. $p = $this->_create_plugin( "<?php\n/*\nPlugin Name: test\nNetwork: true" );
  246. $this->assertTrue( is_network_only_plugin( $p[0] ) );
  247. unlink( $p[1] );
  248. }
  249. /**
  250. * @covers ::activate_plugins
  251. */
  252. public function test_activate_plugins_single_no_array() {
  253. $name = 'hello.php';
  254. activate_plugins( $name );
  255. $this->assertTrue( is_plugin_active( $name ) );
  256. deactivate_plugins( $name );
  257. }
  258. /**
  259. * @covers ::activate_plugins
  260. */
  261. public function test_activate_plugins_single_array() {
  262. $name = 'hello.php';
  263. activate_plugins( array( $name ) );
  264. $this->assertTrue( is_plugin_active( $name ) );
  265. deactivate_plugins( $name );
  266. }
  267. /**
  268. * @covers ::validate_active_plugins
  269. */
  270. public function test_validate_active_plugins_remove_invalid() {
  271. $plugin = $this->_create_plugin();
  272. activate_plugin( $plugin[0] );
  273. unlink( $plugin[1] );
  274. $result = validate_active_plugins();
  275. $this->assertTrue( isset( $result[ $plugin[0] ] ) );
  276. }
  277. /**
  278. * @covers ::validate_active_plugins
  279. */
  280. public function test_validate_active_plugins_empty() {
  281. $this->assertEquals( array(), validate_active_plugins() );
  282. }
  283. /**
  284. * @covers ::is_uninstallable_plugin
  285. */
  286. public function test_is_uninstallable_plugin() {
  287. $this->assertFalse( is_uninstallable_plugin( 'hello' ) );
  288. }
  289. /**
  290. * @covers ::is_uninstallable_plugin
  291. */
  292. public function test_is_uninstallable_plugin_true() {
  293. $plugin = $this->_create_plugin();
  294. $uninstallable_plugins = (array) get_option( 'uninstall_plugins' );
  295. $uninstallable_plugins[ $plugin[0] ] = true;
  296. update_option( 'uninstall_plugins', $uninstallable_plugins );
  297. $this->assertTrue( is_uninstallable_plugin( $plugin[0] ) );
  298. unset( $uninstallable_plugins[ $plugin[0] ] );
  299. update_option( 'uninstall_plugins', $uninstallable_plugins );
  300. unlink( $plugin[1] );
  301. }
  302. /**
  303. * Generate a plugin.
  304. *
  305. * This creates a single-file plugin.
  306. *
  307. * @since 4.2.0
  308. *
  309. * @access private
  310. *
  311. * @param string $data Optional. Data for the plugin file. Default is a dummy plugin header.
  312. * @param string $filename Optional. Filename for the plugin file. Default is a random string.
  313. * @param string $dir_path Optional. Path for directory where the plugin should live.
  314. * @return array Two-membered array of filename and full plugin path.
  315. */
  316. private function _create_plugin( $data = "<?php\n/*\nPlugin Name: Test\n*/", $filename = false, $dir_path = false ) {
  317. if ( false === $filename ) {
  318. $filename = rand_str() . '.php';
  319. }
  320. if ( false === $dir_path ) {
  321. $dir_path = WP_PLUGIN_DIR;
  322. }
  323. $full_name = $dir_path . '/' . wp_unique_filename( $dir_path, $filename );
  324. $file = fopen( $full_name, 'w' );
  325. fwrite( $file, $data );
  326. fclose( $file );
  327. return array( $filename, $full_name );
  328. }
  329. /**
  330. * Move existing mu-plugins to wp-content/mu-plugin/backup.
  331. *
  332. * @since 4.2.0
  333. *
  334. * @access private
  335. */
  336. private function _back_up_mu_plugins() {
  337. if ( is_dir( WPMU_PLUGIN_DIR ) ) {
  338. $mu_bu_dir = WP_CONTENT_DIR . '/mu-plugin-backup';
  339. if ( ! is_dir( $mu_bu_dir ) ) {
  340. mkdir( $mu_bu_dir );
  341. }
  342. $files_to_move = array();
  343. if ( $mu_plugins = opendir( WPMU_PLUGIN_DIR ) ) {
  344. while ( false !== $plugin = readdir( $mu_plugins ) ) {
  345. if ( 0 !== strpos( $plugin, '.' ) ) {
  346. $files_to_move[] = $plugin;
  347. }
  348. }
  349. }
  350. @closedir( $mu_plugins );
  351. foreach ( $files_to_move as $file_to_move ) {
  352. $f = rename( WPMU_PLUGIN_DIR . '/' . $file_to_move, $mu_bu_dir . '/' . $file_to_move );
  353. }
  354. }
  355. }
  356. /**
  357. * Restore backed-up mu-plugins.
  358. *
  359. * @since 4.2.0
  360. *
  361. * @access private
  362. */
  363. private function _restore_mu_plugins() {
  364. $mu_bu_dir = WP_CONTENT_DIR . '/mu-plugin-backup';
  365. $files_to_move = array();
  366. if ( is_dir( $mu_bu_dir ) && $mu_plugins = opendir( $mu_bu_dir ) ) {
  367. while ( false !== $plugin = readdir( $mu_plugins ) ) {
  368. if ( 0 !== strpos( $plugin, '.' ) ) {
  369. $files_to_move[] = $plugin;
  370. }
  371. }
  372. }
  373. @closedir( $mu_plugins );
  374. foreach ( $files_to_move as $file_to_move ) {
  375. rename( $mu_bu_dir . '/' . $file_to_move, WPMU_PLUGIN_DIR . '/' . $file_to_move );
  376. }
  377. if ( is_dir( $mu_bu_dir ) ) {
  378. rmdir( $mu_bu_dir );
  379. }
  380. }
  381. /**
  382. * Move existing drop-ins to wp-content/drop-ins-backup.
  383. *
  384. * @since 4.2.0
  385. *
  386. * @access private
  387. */
  388. private function _back_up_drop_ins() {
  389. $di_bu_dir = WP_CONTENT_DIR . '/drop-ins-backup';
  390. if ( ! is_dir( $di_bu_dir ) ) {
  391. mkdir( $di_bu_dir );
  392. }
  393. foreach ( _get_dropins() as $file_to_move => $v ) {
  394. if ( file_exists( WP_CONTENT_DIR . '/' . $file_to_move ) ) {
  395. rename( WP_CONTENT_DIR . '/' . $file_to_move, $di_bu_dir . '/' . $file_to_move );
  396. }
  397. }
  398. }
  399. /**
  400. * Restore backed-up drop-ins.
  401. *
  402. * @since 4.2.0
  403. *
  404. * @access private
  405. */
  406. private function _restore_drop_ins() {
  407. $di_bu_dir = WP_CONTENT_DIR . '/drop-ins-backup';
  408. foreach ( _get_dropins() as $file_to_move => $v ) {
  409. if ( file_exists( $di_bu_dir . '/' . $file_to_move ) ) {
  410. rename( $di_bu_dir . '/' . $file_to_move, WP_CONTENT_DIR . '/' . $file_to_move );
  411. }
  412. }
  413. if ( is_dir( $di_bu_dir ) ) {
  414. rmdir( $di_bu_dir );
  415. }
  416. }
  417. }