includesSchema.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. /**
  3. * @group admin
  4. */
  5. class Tests_Admin_Includes_Schema extends WP_UnitTestCase {
  6. private static $options;
  7. private static $blogmeta;
  8. private static $sitemeta;
  9. /**
  10. * Make sure the schema code is loaded before the tests are run.
  11. */
  12. public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
  13. global $wpdb;
  14. self::$options = 'testprefix_options';
  15. self::$blogmeta = 'testprefix_blogmeta';
  16. self::$sitemeta = 'testprefix_sitemeta';
  17. $options = self::$options;
  18. $blogmeta = self::$blogmeta;
  19. $sitemeta = self::$sitemeta;
  20. require_once ABSPATH . 'wp-admin/includes/schema.php';
  21. $charset_collate = $wpdb->get_charset_collate();
  22. $max_index_length = 191;
  23. // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
  24. $wpdb->query(
  25. "
  26. CREATE TABLE {$options} (
  27. option_id bigint(20) unsigned NOT NULL auto_increment,
  28. option_name varchar(191) NOT NULL default '',
  29. option_value longtext NOT NULL,
  30. autoload varchar(20) NOT NULL default 'yes',
  31. PRIMARY KEY (option_id),
  32. UNIQUE KEY option_name (option_name)
  33. ) {$charset_collate}
  34. "
  35. );
  36. $wpdb->query(
  37. "
  38. CREATE TABLE {$blogmeta} (
  39. meta_id bigint(20) unsigned NOT NULL auto_increment,
  40. blog_id bigint(20) unsigned NOT NULL default '0',
  41. meta_key varchar(255) default NULL,
  42. meta_value longtext,
  43. PRIMARY KEY (meta_id),
  44. KEY meta_key (meta_key({$max_index_length})),
  45. KEY blog_id (blog_id)
  46. ) {$charset_collate}
  47. "
  48. );
  49. $wpdb->query(
  50. "
  51. CREATE TABLE {$sitemeta} (
  52. meta_id bigint(20) unsigned NOT NULL auto_increment,
  53. site_id bigint(20) unsigned NOT NULL default '0',
  54. meta_key varchar(255) default NULL,
  55. meta_value longtext,
  56. PRIMARY KEY (meta_id),
  57. KEY meta_key (meta_key({$max_index_length})),
  58. KEY site_id (site_id)
  59. ) {$charset_collate}
  60. "
  61. );
  62. // phpcs:enable
  63. }
  64. /**
  65. * Drop tables that were created before running the tests.
  66. */
  67. public static function wpTearDownAfterClass() {
  68. global $wpdb;
  69. $options = self::$options;
  70. $blogmeta = self::$blogmeta;
  71. $sitemeta = self::$sitemeta;
  72. // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
  73. $wpdb->query( "DROP TABLE IF EXISTS {$options}" );
  74. $wpdb->query( "DROP TABLE IF EXISTS {$blogmeta}" );
  75. $wpdb->query( "DROP TABLE IF EXISTS {$sitemeta}" );
  76. // phpcs:enable
  77. }
  78. /**
  79. * @ticket 44893
  80. * @dataProvider data_populate_options
  81. */
  82. function test_populate_options( $options, $expected ) {
  83. global $wpdb;
  84. $orig_options = $wpdb->options;
  85. $wpdb->options = self::$options;
  86. populate_options( $options );
  87. wp_cache_delete( 'alloptions', 'options' );
  88. $results = array();
  89. foreach ( $expected as $option => $value ) {
  90. $results[ $option ] = get_option( $option );
  91. }
  92. $wpdb->query( "TRUNCATE TABLE {$wpdb->options}" );
  93. $wpdb->options = $orig_options;
  94. $this->assertSame( $expected, $results );
  95. }
  96. public function data_populate_options() {
  97. return array(
  98. array(
  99. array(),
  100. array(
  101. // Random options to check.
  102. 'posts_per_rss' => '10',
  103. 'rss_use_excerpt' => '0',
  104. 'mailserver_url' => 'mail.example.com',
  105. 'mailserver_login' => 'login@example.com',
  106. 'mailserver_pass' => 'password',
  107. ),
  108. ),
  109. array(
  110. array(
  111. 'posts_per_rss' => '7',
  112. 'rss_use_excerpt' => '1',
  113. ),
  114. array(
  115. // Random options to check.
  116. 'posts_per_rss' => '7',
  117. 'rss_use_excerpt' => '1',
  118. 'mailserver_url' => 'mail.example.com',
  119. 'mailserver_login' => 'login@example.com',
  120. 'mailserver_pass' => 'password',
  121. ),
  122. ),
  123. array(
  124. array(
  125. 'custom_option' => '1',
  126. ),
  127. array(
  128. // Random options to check.
  129. 'custom_option' => '1',
  130. 'posts_per_rss' => '10',
  131. 'rss_use_excerpt' => '0',
  132. 'mailserver_url' => 'mail.example.com',
  133. 'mailserver_login' => 'login@example.com',
  134. 'mailserver_pass' => 'password',
  135. ),
  136. ),
  137. array(
  138. array(
  139. 'use_quicktags' => '1',
  140. ),
  141. array(
  142. // This option is disallowed and should never exist.
  143. 'use_quicktags' => false,
  144. ),
  145. ),
  146. array(
  147. array(
  148. 'rss_0123456789abcdef0123456789abcdef' => '1',
  149. 'rss_0123456789abcdef0123456789abcdef_ts' => '1',
  150. ),
  151. array(
  152. // These options would be obsolete magpie cache data and should never exist.
  153. 'rss_0123456789abcdef0123456789abcdef' => false,
  154. 'rss_0123456789abcdef0123456789abcdef_ts' => false,
  155. ),
  156. ),
  157. );
  158. }
  159. /**
  160. * @ticket 44896
  161. * @group multisite
  162. * @group ms-required
  163. * @dataProvider data_populate_site_meta
  164. */
  165. function test_populate_site_meta( $meta, $expected ) {
  166. global $wpdb;
  167. $orig_blogmeta = $wpdb->blogmeta;
  168. $wpdb->blogmeta = self::$blogmeta;
  169. populate_site_meta( 42, $meta );
  170. $results = array();
  171. foreach ( $expected as $meta_key => $value ) {
  172. $results[ $meta_key ] = get_site_meta( 42, $meta_key, true );
  173. }
  174. $wpdb->query( "TRUNCATE TABLE {$wpdb->blogmeta}" );
  175. $wpdb->blogmeta = $orig_blogmeta;
  176. $this->assertSame( $expected, $results );
  177. }
  178. public function data_populate_site_meta() {
  179. return array(
  180. array(
  181. array(),
  182. array(
  183. 'unknown_value' => '',
  184. ),
  185. ),
  186. array(
  187. array(
  188. 'custom_meta' => '1',
  189. ),
  190. array(
  191. 'custom_meta' => '1',
  192. ),
  193. ),
  194. );
  195. }
  196. /**
  197. * @ticket 44895
  198. * @group multisite
  199. * @dataProvider data_populate_network_meta
  200. */
  201. function test_populate_network_meta( $meta, $expected ) {
  202. global $wpdb;
  203. $orig_sitemeta = $wpdb->sitemeta;
  204. $wpdb->sitemeta = self::$sitemeta;
  205. populate_network_meta( 42, $meta );
  206. $results = array();
  207. foreach ( $expected as $meta_key => $value ) {
  208. if ( is_multisite() ) {
  209. $results[ $meta_key ] = get_network_option( 42, $meta_key );
  210. } else {
  211. $results[ $meta_key ] = $wpdb->get_var( $wpdb->prepare( "SELECT meta_value FROM {$wpdb->sitemeta} WHERE meta_key = %s AND site_id = %d", $meta_key, 42 ) );
  212. }
  213. }
  214. $wpdb->query( "TRUNCATE TABLE {$wpdb->sitemeta}" );
  215. $wpdb->sitemeta = $orig_sitemeta;
  216. $this->assertSame( $expected, $results );
  217. }
  218. public function data_populate_network_meta() {
  219. return array(
  220. array(
  221. array(),
  222. array(
  223. // Random meta to check.
  224. 'registration' => 'none',
  225. 'blog_upload_space' => '100',
  226. 'fileupload_maxk' => '1500',
  227. ),
  228. ),
  229. array(
  230. array(
  231. 'site_name' => 'My Great Network',
  232. 'WPLANG' => 'fr_FR',
  233. ),
  234. array(
  235. // Random meta to check.
  236. 'site_name' => 'My Great Network',
  237. 'registration' => 'none',
  238. 'blog_upload_space' => '100',
  239. 'fileupload_maxk' => '1500',
  240. 'WPLANG' => 'fr_FR',
  241. ),
  242. ),
  243. array(
  244. array(
  245. 'custom_meta' => '1',
  246. ),
  247. array(
  248. // Random meta to check.
  249. 'custom_meta' => '1',
  250. 'registration' => 'none',
  251. 'blog_upload_space' => '100',
  252. 'fileupload_maxk' => '1500',
  253. ),
  254. ),
  255. );
  256. }
  257. }