ms.php 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  1. <?php
  2. if ( is_multisite() ) :
  3. /**
  4. * A set of unit tests for WordPress Multisite
  5. *
  6. * @group multisite
  7. */
  8. class Tests_MS extends WP_UnitTestCase {
  9. protected $plugin_hook_count = 0;
  10. protected $suppress = false;
  11. function setUp() {
  12. global $wpdb;
  13. parent::setUp();
  14. $this->suppress = $wpdb->suppress_errors();
  15. $_SERVER['REMOTE_ADDR'] = '';
  16. }
  17. function tearDown() {
  18. global $wpdb;
  19. parent::tearDown();
  20. $wpdb->suppress_errors( $this->suppress );
  21. }
  22. /**
  23. * @ticket 22917
  24. */
  25. function test_enable_live_network_site_counts_filter() {
  26. $site_count_start = get_blog_count();
  27. // false for large networks by default
  28. add_filter( 'enable_live_network_counts', '__return_false' );
  29. $this->factory->blog->create_many( 4 );
  30. // count only updated when cron runs, so unchanged
  31. $this->assertEquals( $site_count_start, (int) get_blog_count() );
  32. add_filter( 'enable_live_network_counts', '__return_true' );
  33. $site_ids = $this->factory->blog->create_many( 4 );
  34. $this->assertEquals( $site_count_start + 9, (int) get_blog_count() );
  35. //clean up
  36. remove_filter( 'enable_live_network_counts', '__return_false' );
  37. remove_filter( 'enable_live_network_counts', '__return_true' );
  38. foreach ( $site_ids as $site_id ) {
  39. wpmu_delete_blog( $site_id, true );
  40. }
  41. }
  42. /**
  43. * @ticket 22917
  44. */
  45. function test_enable_live_network_user_counts_filter() {
  46. // false for large networks by default
  47. add_filter( 'enable_live_network_counts', '__return_false' );
  48. // Refresh the cache
  49. wp_update_network_counts();
  50. $start_count = get_user_count();
  51. wpmu_create_user( 'user', 'pass', 'email' );
  52. // No change, cache not refreshed
  53. $count = get_user_count();
  54. $this->assertEquals( $start_count, $count );
  55. wp_update_network_counts();
  56. $start_count = get_user_count();
  57. add_filter( 'enable_live_network_counts', '__return_true' );
  58. wpmu_create_user( 'user2', 'pass2', 'email2' );
  59. $count = get_user_count();
  60. $this->assertEquals( $start_count + 1, $count );
  61. remove_filter( 'enable_live_network_counts', '__return_false' );
  62. remove_filter( 'enable_live_network_counts', '__return_true' );
  63. }
  64. function test_create_and_delete_blog() {
  65. global $wpdb;
  66. $blog_ids = $this->factory->blog->create_many( 4 );
  67. foreach ( $blog_ids as $blog_id ) {
  68. $this->assertInternalType( 'int', $blog_id );
  69. $prefix = $wpdb->get_blog_prefix( $blog_id );
  70. // $get_all = false
  71. $details = get_blog_details( $blog_id, false );
  72. $this->assertEquals( $details, wp_cache_get( $blog_id . 'short', 'blog-details' ) );
  73. // get_id_from_blogname(), see #20950
  74. $this->assertEquals( $blog_id, get_id_from_blogname( $details->path ) );
  75. $this->assertEquals( $blog_id, wp_cache_get( 'get_id_from_blogname_' . trim( $details->path, '/' ), 'blog-details' ) );
  76. // get_blog_id_from_url()
  77. $this->assertEquals( $blog_id, get_blog_id_from_url( $details->domain, $details->path ) );
  78. $key = md5( $details->domain . $details->path );
  79. $this->assertEquals( $blog_id, wp_cache_get( $key, 'blog-id-cache' ) );
  80. // These are empty until get_blog_details() is called with $get_all = true
  81. $this->assertEquals( false, wp_cache_get( $blog_id, 'blog-details' ) );
  82. $key = md5( $details->domain . $details->path );
  83. $this->assertEquals( false, wp_cache_get( $key, 'blog-lookup' ) );
  84. // $get_all = true should propulate the full blog-details cache and the blog slug lookup cache
  85. $details = get_blog_details( $blog_id, true );
  86. $this->assertEquals( $details, wp_cache_get( $blog_id, 'blog-details' ) );
  87. $this->assertEquals( $details, wp_cache_get( $key, 'blog-lookup' ) );
  88. foreach ( $wpdb->tables( 'blog', false ) as $table ) {
  89. $suppress = $wpdb->suppress_errors();
  90. $table_fields = $wpdb->get_results( "DESCRIBE $prefix$table;" );
  91. $wpdb->suppress_errors( $suppress );
  92. $this->assertNotEmpty( $table_fields );
  93. $result = $wpdb->get_results( "SELECT * FROM $prefix$table LIMIT 1" );
  94. if ( 'commentmeta' == $table || 'links' == $table )
  95. $this->assertEmpty( $result );
  96. else
  97. $this->assertNotEmpty( $result );
  98. }
  99. }
  100. // update the blog count cache to use get_blog_count()
  101. wp_update_network_counts();
  102. $this->assertEquals( 4 + 1, (int) get_blog_count() );
  103. $drop_tables = false;
  104. // delete all blogs
  105. foreach ( $blog_ids as $blog_id ) {
  106. // drop tables for every second blog
  107. $drop_tables = ! $drop_tables;
  108. $details = get_blog_details( $blog_id, false );
  109. wpmu_delete_blog( $blog_id, $drop_tables );
  110. $this->assertEquals( false, wp_cache_get( 'get_id_from_blogname_' . trim( $details->path, '/' ), 'blog-details' ) );
  111. $this->assertEquals( false, wp_cache_get( $blog_id, 'blog-details' ) );
  112. $this->assertEquals( false, wp_cache_get( $blog_id . 'short', 'blog-details' ) );
  113. $key = md5( $details->domain . $details->path );
  114. $this->assertEquals( false, wp_cache_get( $key, 'blog-lookup' ) );
  115. $this->assertEquals( false, wp_cache_get( $key, 'blog-id-cache' ) );
  116. $prefix = $wpdb->get_blog_prefix( $blog_id );
  117. foreach ( $wpdb->tables( 'blog', false ) as $table ) {
  118. $suppress = $wpdb->suppress_errors();
  119. $table_fields = $wpdb->get_results( "DESCRIBE $prefix$table;" );
  120. $wpdb->suppress_errors( $suppress );
  121. if ( $drop_tables )
  122. $this->assertEmpty( $table_fields );
  123. else
  124. $this->assertNotEmpty( $table_fields, $prefix . $table );
  125. }
  126. }
  127. // update the blog count cache to use get_blog_count()
  128. wp_update_network_counts();
  129. $this->assertEquals( 1, get_blog_count() );
  130. }
  131. function test_get_blogs_of_user() {
  132. // Logged out users don't have blogs.
  133. $this->assertEquals( array(), get_blogs_of_user( 0 ) );
  134. $user1_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
  135. $blog_ids = $this->factory->blog->create_many( 10, array( 'user_id' => $user1_id ) );
  136. foreach ( $blog_ids as $blog_id )
  137. $this->assertInternalType( 'int', $blog_id );
  138. $blogs_of_user = array_keys( get_blogs_of_user( $user1_id, false ) );
  139. sort( $blogs_of_user );
  140. $this->assertEquals ( array_merge( array( 1 ), $blog_ids), $blogs_of_user );
  141. $this->assertTrue( remove_user_from_blog( $user1_id, 1 ) );
  142. $blogs_of_user = array_keys( get_blogs_of_user( $user1_id, false ) );
  143. sort( $blogs_of_user );
  144. $this->assertEquals ( $blog_ids, $blogs_of_user );
  145. foreach ( get_blogs_of_user( $user1_id, false ) as $blog ) {
  146. $this->assertTrue( isset( $blog->userblog_id ) );
  147. $this->assertTrue( isset( $blog->blogname ) );
  148. $this->assertTrue( isset( $blog->domain ) );
  149. $this->assertTrue( isset( $blog->path ) );
  150. $this->assertTrue( isset( $blog->site_id ) );
  151. $this->assertTrue( isset( $blog->siteurl ) );
  152. $this->assertTrue( isset( $blog->archived ) );
  153. $this->assertTrue( isset( $blog->spam ) );
  154. $this->assertTrue( isset( $blog->deleted ) );
  155. }
  156. // Non-existent users don't have blogs.
  157. wpmu_delete_user( $user1_id );
  158. $user = new WP_User( $user1_id );
  159. $this->assertFalse( $user->exists(), 'WP_User->exists' );
  160. $this->assertEquals( array(), get_blogs_of_user( $user1_id ) );
  161. }
  162. /**
  163. * @expectedDeprecated is_blog_user
  164. */
  165. function test_is_blog_user() {
  166. global $wpdb;
  167. $user1_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
  168. $old_current = get_current_user_id();
  169. wp_set_current_user( $user1_id );
  170. $this->assertTrue( is_blog_user() );
  171. $this->assertTrue( is_blog_user( $wpdb->blogid ) );
  172. $blog_ids = array();
  173. $blog_ids = $this->factory->blog->create_many( 5 );
  174. foreach ( $blog_ids as $blog_id ) {
  175. $this->assertInternalType( 'int', $blog_id );
  176. $this->assertTrue( is_blog_user( $blog_id ) );
  177. $this->assertTrue( remove_user_from_blog( $user1_id, $blog_id ) );
  178. $this->assertFalse( is_blog_user( $blog_id ) );
  179. }
  180. wp_set_current_user( $old_current );
  181. }
  182. function test_is_user_member_of_blog() {
  183. global $wpdb;
  184. $user1_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
  185. $old_current = get_current_user_id();
  186. wp_set_current_user( $user1_id );
  187. $this->assertTrue( is_user_member_of_blog() );
  188. $this->assertTrue( is_user_member_of_blog( 0, 0 ) );
  189. $this->assertTrue( is_user_member_of_blog( 0, $wpdb->blogid ) );
  190. $this->assertTrue( is_user_member_of_blog( $user1_id ) );
  191. $this->assertTrue( is_user_member_of_blog( $user1_id, $wpdb->blogid ) );
  192. $blog_ids = $this->factory->blog->create_many( 5 );
  193. foreach ( $blog_ids as $blog_id ) {
  194. $this->assertInternalType( 'int', $blog_id );
  195. $this->assertTrue( is_user_member_of_blog( $user1_id, $blog_id ) );
  196. $this->assertTrue( remove_user_from_blog( $user1_id, $blog_id ) );
  197. $this->assertFalse( is_user_member_of_blog( $user1_id, $blog_id ) );
  198. }
  199. wpmu_delete_user( $user1_id );
  200. $user = new WP_User( $user1_id );
  201. $this->assertFalse( $user->exists(), 'WP_User->exists' );
  202. $this->assertFalse( is_user_member_of_blog( $user1_id ), 'is_user_member_of_blog' );
  203. wp_set_current_user( $old_current );
  204. }
  205. function test_active_network_plugins() {
  206. $path = "hello.php";
  207. // local activate, should be invisible for the network
  208. activate_plugin($path); // $network_wide = false
  209. $active_plugins = wp_get_active_network_plugins();
  210. $this->assertEquals( Array(), $active_plugins );
  211. add_action( 'deactivated_plugin', array( $this, '_helper_deactivate_hook' ) );
  212. // activate the plugin sitewide
  213. activate_plugin($path, '', $network_wide = true);
  214. $active_plugins = wp_get_active_network_plugins();
  215. $this->assertEquals( Array(WP_PLUGIN_DIR . '/hello.php'), $active_plugins );
  216. //deactivate the plugin
  217. deactivate_plugins($path);
  218. $active_plugins = wp_get_active_network_plugins();
  219. $this->assertEquals( Array(), $active_plugins );
  220. $this->assertEquals( 1, $this->plugin_hook_count ); // testing actions and silent mode
  221. activate_plugin($path, '', $network_wide = true);
  222. deactivate_plugins($path, true); // silent
  223. $this->assertEquals( 1, $this->plugin_hook_count ); // testing actions and silent mode
  224. }
  225. function _helper_deactivate_hook() {
  226. $this->plugin_hook_count++;
  227. }
  228. function test_get_user_count() {
  229. // Refresh the cache
  230. wp_update_network_counts();
  231. $start_count = get_user_count();
  232. // Only false for large networks as of 3.7
  233. add_filter( 'enable_live_network_counts', '__return_false' );
  234. $this->factory->user->create( array( 'role' => 'administrator' ) );
  235. $count = get_user_count(); // No change, cache not refreshed
  236. $this->assertEquals( $start_count, $count );
  237. wp_update_network_counts(); // Magic happens here
  238. $count = get_user_count();
  239. $this->assertEquals( $start_count + 1, $count );
  240. remove_filter( 'enable_live_network_counts', '__return_false' );
  241. }
  242. function test_wp_schedule_update_network_counts() {
  243. $this->assertFalse(wp_next_scheduled('update_network_counts'));
  244. // We can't use wp_schedule_update_network_counts() because WP_INSTALLING is set
  245. wp_schedule_event(time(), 'twicedaily', 'update_network_counts');
  246. $this->assertInternalType('int', wp_next_scheduled('update_network_counts'));
  247. }
  248. function test_users_can_register_signup_filter() {
  249. $registration = get_site_option('registration');
  250. $this->assertFalse( users_can_register_signup_filter() );
  251. update_site_option('registration', 'all');
  252. $this->assertTrue( users_can_register_signup_filter() );
  253. update_site_option('registration', 'user');
  254. $this->assertTrue( users_can_register_signup_filter() );
  255. update_site_option('registration', 'none');
  256. $this->assertFalse( users_can_register_signup_filter() );
  257. }
  258. /**
  259. * @expectedDeprecated get_dashboard_blog
  260. */
  261. function test_get_dashboard_blog() {
  262. // if there is no dashboard blog set, current blog is used
  263. $dashboard_blog = get_dashboard_blog();
  264. $this->assertEquals( 1, $dashboard_blog->blog_id );
  265. $user_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
  266. $blog_id = $this->factory->blog->create( array( 'user_id' => $user_id ) );
  267. $this->assertInternalType( 'int', $blog_id );
  268. // set the dashboard blog to another one
  269. update_site_option( 'dashboard_blog', $blog_id );
  270. $dashboard_blog = get_dashboard_blog();
  271. $this->assertEquals( $blog_id, $dashboard_blog->blog_id );
  272. }
  273. function test_wpmu_log_new_registrations() {
  274. global $wpdb;
  275. $user = new WP_User( 1 );
  276. $ip = preg_replace( '/[^0-9., ]/', '',$_SERVER['REMOTE_ADDR'] );
  277. wpmu_log_new_registrations(1,1);
  278. // currently there is no wrapper function for the registration_log
  279. $reg_blog = $wpdb->get_col( "SELECT email FROM {$wpdb->registration_log} WHERE {$wpdb->registration_log}.blog_id = 1 AND IP LIKE '" . $ip . "'" );
  280. $this->assertEquals( $user->user_email, $reg_blog[ count( $reg_blog )-1 ] );
  281. }
  282. /**
  283. * @ticket 18119
  284. */
  285. function test_upload_is_user_over_quota() {
  286. $default_space_allowed = 100;
  287. $echo = false;
  288. $this->assertFalse( upload_is_user_over_quota( $echo ) );
  289. $this->assertTrue( is_upload_space_available() );
  290. update_site_option('upload_space_check_disabled', true);
  291. $this->assertFalse( upload_is_user_over_quota( $echo ) );
  292. $this->assertTrue( is_upload_space_available() );
  293. update_site_option( 'blog_upload_space', 0 );
  294. $this->assertFalse( upload_is_user_over_quota( $echo ) );
  295. $this->assertEquals( $default_space_allowed, get_space_allowed() );
  296. $this->assertTrue( is_upload_space_available() );
  297. update_site_option('upload_space_check_disabled', false);
  298. $this->assertFalse( upload_is_user_over_quota( $echo ) );
  299. $this->assertTrue( is_upload_space_available() );
  300. if ( defined( 'BLOGSUPLOADDIR' ) && ! file_exists( BLOGSUPLOADDIR ) )
  301. $this->markTestSkipped( 'This test is broken when blogs.dir does not exist. ');
  302. /*
  303. This is broken when blogs.dir does not exist, as get_upload_space_available()
  304. simply returns the value of blog_upload_space (converted to bytes), which would
  305. be negative but still not false. When blogs.dir does exist, < 0 is returned as 0.
  306. */
  307. update_site_option( 'blog_upload_space', -1 );
  308. $this->assertTrue( upload_is_user_over_quota( $echo ) );
  309. $this->assertEquals( -1, get_space_allowed() );
  310. $this->assertFalse( is_upload_space_available() );
  311. update_option( 'blog_upload_space', 0 );
  312. $this->assertFalse( upload_is_user_over_quota( $echo ) );
  313. $this->assertEquals( $default_space_allowed, get_space_allowed() );
  314. $this->assertTrue( is_upload_space_available() );
  315. update_option( 'blog_upload_space', -1 );
  316. $this->assertTrue( upload_is_user_over_quota( $echo ) );
  317. $this->assertEquals( -1, get_space_allowed() );
  318. $this->assertFalse( is_upload_space_available() );
  319. }
  320. function test_wpmu_update_blogs_date() {
  321. global $wpdb;
  322. wpmu_update_blogs_date();
  323. // compare the update time with the current time, allow delta < 2
  324. $blog = get_blog_details( $wpdb->blogid );
  325. $current_time = time();
  326. $time_difference = $current_time - strtotime( $blog->last_updated );
  327. $this->assertLessThan( 2, $time_difference );
  328. }
  329. function test_getters(){
  330. global $current_site;
  331. $blog_id = get_current_blog_id();
  332. $blog = get_blog_details( $blog_id );
  333. $this->assertEquals( $blog_id, $blog->blog_id );
  334. $this->assertEquals( $current_site->domain, $blog->domain );
  335. $this->assertEquals( '/', $blog->path );
  336. // Test defaulting to current blog
  337. $this->assertEquals( $blog, get_blog_details() );
  338. $user_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
  339. $blog_id = $this->factory->blog->create( array( 'user_id' => $user_id, 'path' => '/test_blogname', 'title' => 'Test Title' ) );
  340. $this->assertInternalType( 'int', $blog_id );
  341. $this->assertEquals( 'http://' . DOMAIN_CURRENT_SITE . PATH_CURRENT_SITE . 'test_blogname/', get_blogaddress_by_name('test_blogname') );
  342. $this->assertEquals( $blog_id, get_id_from_blogname('test_blogname') );
  343. }
  344. function _action_counter_cb( $blog_id ) {
  345. global $test_action_counter;
  346. $test_action_counter++;
  347. }
  348. function test_update_blog_details() {
  349. global $test_action_counter;
  350. $user_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
  351. $blog_id = $this->factory->blog->create( array( 'user_id' => $user_id, 'path' => '/test_blogpath', 'title' => 'Test Title' ) );
  352. $this->assertInternalType( 'int', $blog_id );
  353. $result = update_blog_details( $blog_id, array('domain' => 'example.com', 'path' => 'my_path/') );
  354. $this->assertTrue( $result );
  355. $blog = get_blog_details( $blog_id );
  356. $this->assertEquals( 'example.com', $blog->domain );
  357. $this->assertEquals( 'my_path/', $blog->path );
  358. $this->assertEquals( '0', $blog->spam );
  359. $result = update_blog_details( $blog_id, array('domain' => 'example2.com','spam' => 1) );
  360. $this->assertTrue( $result );
  361. $blog = get_blog_details( $blog_id );
  362. $this->assertEquals( 'example2.com', $blog->domain );
  363. $this->assertEquals( 'my_path/', $blog->path );
  364. $this->assertEquals( '1', $blog->spam );
  365. $result = update_blog_details( $blog_id );
  366. $this->assertFalse( $result );
  367. $blog = get_blog_details( $blog_id );
  368. $this->assertEquals( 'example2.com', $blog->domain );
  369. $this->assertEquals( 'my_path/', $blog->path );
  370. $this->assertEquals( '1', $blog->spam );
  371. $test_action_counter = 0;
  372. add_action( 'make_ham_blog', array( $this, '_action_counter_cb' ), 10, 1 );
  373. $result = update_blog_details( $blog_id, array( 'spam' => 0 ) );
  374. $this->assertTrue( $result );
  375. $blog = get_blog_details( $blog_id );
  376. $this->assertEquals( '0', $blog->spam );
  377. $this->assertEquals( 1, $test_action_counter );
  378. // Same again
  379. $result = update_blog_details( $blog_id, array( 'spam' => 0 ) );
  380. $this->assertTrue( $result );
  381. $blog = get_blog_details( $blog_id );
  382. $this->assertEquals( '0', $blog->spam );
  383. $this->assertEquals( 1, $test_action_counter );
  384. remove_action( 'make_ham_blog', array( $this, '_action_counter_cb' ), 10, 1 );
  385. add_action( 'make_spam_blog', array( $this, '_action_counter_cb' ), 10, 1 );
  386. $result = update_blog_details( $blog_id, array( 'spam' => 1 ) );
  387. $this->assertTrue( $result );
  388. $blog = get_blog_details( $blog_id );
  389. $this->assertEquals( '1', $blog->spam );
  390. $this->assertEquals( 2, $test_action_counter );
  391. // Same again
  392. $result = update_blog_details( $blog_id, array( 'spam' => 1 ) );
  393. $this->assertTrue( $result );
  394. $blog = get_blog_details( $blog_id );
  395. $this->assertEquals( '1', $blog->spam );
  396. $this->assertEquals( 2, $test_action_counter );
  397. remove_action( 'make_spam_blog', array( $this, '_action_counter_cb' ), 10, 1 );
  398. add_action( 'archive_blog', array( $this, '_action_counter_cb' ), 10, 1 );
  399. $result = update_blog_details( $blog_id, array( 'archived' => 1 ) );
  400. $this->assertTrue( $result );
  401. $blog = get_blog_details( $blog_id );
  402. $this->assertEquals( '1', $blog->archived );
  403. $this->assertEquals( 3, $test_action_counter );
  404. // Same again
  405. $result = update_blog_details( $blog_id, array( 'archived' => 1 ) );
  406. $this->assertTrue( $result );
  407. $blog = get_blog_details( $blog_id );
  408. $this->assertEquals( '1', $blog->archived );
  409. $this->assertEquals( 3, $test_action_counter );
  410. remove_action( 'archive_blog', array( $this, '_action_counter_cb' ), 10, 1 );
  411. add_action( 'unarchive_blog', array( $this, '_action_counter_cb' ), 10, 1 );
  412. $result = update_blog_details( $blog_id, array( 'archived' => 0 ) );
  413. $this->assertTrue( $result );
  414. $blog = get_blog_details( $blog_id );
  415. $this->assertEquals( '0', $blog->archived );
  416. $this->assertEquals( 4, $test_action_counter );
  417. // Same again
  418. $result = update_blog_details( $blog_id, array( 'archived' => 0 ) );
  419. $this->assertTrue( $result );
  420. $blog = get_blog_details( $blog_id );
  421. $this->assertEquals( '0', $blog->archived );
  422. $this->assertEquals( 4, $test_action_counter );
  423. remove_action( 'unarchive_blog', array( $this, '_action_counter_cb' ), 10, 1 );
  424. add_action( 'make_delete_blog', array( $this, '_action_counter_cb' ), 10, 1 );
  425. $result = update_blog_details( $blog_id, array( 'deleted' => 1 ) );
  426. $this->assertTrue( $result );
  427. $blog = get_blog_details( $blog_id );
  428. $this->assertEquals( '1', $blog->deleted );
  429. $this->assertEquals( 5, $test_action_counter );
  430. // Same again
  431. $result = update_blog_details( $blog_id, array( 'deleted' => 1 ) );
  432. $this->assertTrue( $result );
  433. $blog = get_blog_details( $blog_id );
  434. $this->assertEquals( '1', $blog->deleted );
  435. $this->assertEquals( 5, $test_action_counter );
  436. remove_action( 'make_delete_blog', array( $this, '_action_counter_cb' ), 10, 1 );
  437. add_action( 'make_undelete_blog', array( $this, '_action_counter_cb' ), 10, 1 );
  438. $result = update_blog_details( $blog_id, array( 'deleted' => 0 ) );
  439. $this->assertTrue( $result );
  440. $blog = get_blog_details( $blog_id );
  441. $this->assertEquals( '0', $blog->deleted );
  442. $this->assertEquals( 6, $test_action_counter );
  443. // Same again
  444. $result = update_blog_details( $blog_id, array( 'deleted' => 0 ) );
  445. $this->assertTrue( $result );
  446. $blog = get_blog_details( $blog_id );
  447. $this->assertEquals( '0', $blog->deleted );
  448. $this->assertEquals( 6, $test_action_counter );
  449. remove_action( 'make_undelete_blog', array( $this, '_action_counter_cb' ), 10, 1 );
  450. add_action( 'mature_blog', array( $this, '_action_counter_cb' ), 10, 1 );
  451. $result = update_blog_details( $blog_id, array( 'mature' => 1 ) );
  452. $this->assertTrue( $result );
  453. $blog = get_blog_details( $blog_id );
  454. $this->assertEquals( '1', $blog->mature );
  455. $this->assertEquals( 7, $test_action_counter );
  456. // Same again
  457. $result = update_blog_details( $blog_id, array( 'mature' => 1 ) );
  458. $this->assertTrue( $result );
  459. $blog = get_blog_details( $blog_id );
  460. $this->assertEquals( '1', $blog->mature );
  461. $this->assertEquals( 7, $test_action_counter );
  462. remove_action( 'mature_blog', array( $this, '_action_counter_cb' ), 10, 1 );
  463. add_action( 'unmature_blog', array( $this, '_action_counter_cb' ), 10, 1 );
  464. $result = update_blog_details( $blog_id, array( 'mature' => 0 ) );
  465. $this->assertTrue( $result );
  466. $blog = get_blog_details( $blog_id );
  467. $this->assertEquals( '0', $blog->mature );
  468. $this->assertEquals( 8, $test_action_counter );
  469. // Same again
  470. $result = update_blog_details( $blog_id, array( 'mature' => 0 ) );
  471. $this->assertTrue( $result );
  472. $blog = get_blog_details( $blog_id );
  473. $this->assertEquals( '0', $blog->mature );
  474. $this->assertEquals( 8, $test_action_counter );
  475. remove_action( 'unmature_blog', array( $this, '_action_counter_cb' ), 10, 1 );
  476. }
  477. /**
  478. * Test fetching a blog that doesn't exist and again after it exists.
  479. *
  480. * @ticket 23405
  481. */
  482. function test_get_blog_details_blog_does_not_exist() {
  483. global $wpdb;
  484. $blog_id = $wpdb->get_var( "SELECT MAX(blog_id) FROM $wpdb->blogs" );
  485. // An idosyncrancy of the unit tests is that the max blog_id gets reset
  486. // to 1 in between test cases but picks up where it previously left off
  487. // on the next insert. If 1 is reported, burn a blog create to get
  488. // the max counter back in sync.
  489. if ( 1 == $blog_id ) {
  490. $blog_id = $this->factory->blog->create();
  491. }
  492. $blog_id++;
  493. $this->assertFalse( wp_cache_get( $blog_id, 'blog-details' ) );
  494. $this->assertFalse( get_blog_details( $blog_id ) );
  495. $this->assertEquals( -1, wp_cache_get( $blog_id, 'blog-details' ) );
  496. $this->assertFalse( get_blog_details( $blog_id ) );
  497. $this->assertEquals( -1, wp_cache_get( $blog_id, 'blog-details' ) );
  498. $this->assertEquals( $blog_id, $this->factory->blog->create() );
  499. $this->assertFalse( wp_cache_get( $blog_id, 'blog-details' ) );
  500. $blog = get_blog_details( $blog_id );
  501. $this->assertEquals( $blog_id, $blog->blog_id );
  502. $this->assertEquals( $blog, wp_cache_get( $blog_id, 'blog-details' ) );
  503. wpmu_delete_blog( $blog_id );
  504. $this->assertFalse( wp_cache_get( $blog_id, 'blog-details' ) );
  505. $blog->deleted = '1';
  506. $this->assertEQuals( $blog, get_blog_details( $blog_id ) );
  507. $this->assertEquals( $blog, wp_cache_get( $blog_id, 'blog-details' ) );
  508. wpmu_delete_blog( $blog_id, true );
  509. $this->assertFalse( get_blog_details( $blog_id ) );
  510. $this->assertEquals( -1, wp_cache_get( $blog_id, 'blog-details' ) );
  511. }
  512. function test_update_blog_status() {
  513. global $test_action_counter;
  514. $user_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
  515. $blog_id = $this->factory->blog->create( array( 'user_id' => $user_id, 'path' => '/test_blogpath', 'title' => 'Test Title' ) );
  516. $this->assertInternalType( 'int', $blog_id );
  517. $test_action_counter = 0;
  518. $count = 1;
  519. add_action( 'make_ham_blog', array( $this, '_action_counter_cb' ), 10, 1 );
  520. $result = update_blog_status( $blog_id, 'spam', 0 );
  521. $this->assertEquals( 0, $result );
  522. $blog = get_blog_details( $blog_id );
  523. $this->assertEquals( '0', $blog->spam );
  524. $this->assertEquals( $count, $test_action_counter );
  525. // Same again
  526. $count++;
  527. $result = update_blog_status( $blog_id, 'spam', 0 );
  528. $this->assertEquals( 0, $result );
  529. $blog = get_blog_details( $blog_id );
  530. $this->assertEquals( '0', $blog->spam );
  531. $this->assertEquals( $count, $test_action_counter );
  532. remove_action( 'make_ham_blog', array( $this, '_action_counter_cb' ), 10, 1 );
  533. $count++;
  534. add_action( 'make_spam_blog', array( $this, '_action_counter_cb' ), 10, 1 );
  535. $result = update_blog_status( $blog_id, 'spam', 1 );
  536. $this->assertEquals( 1, $result );
  537. $blog = get_blog_details( $blog_id );
  538. $this->assertEquals( '1', $blog->spam );
  539. $this->assertEquals( $count, $test_action_counter );
  540. // Same again
  541. $count++;
  542. $result = update_blog_status( $blog_id, 'spam', 1 );
  543. $this->assertEquals( 1, $result );
  544. $blog = get_blog_details( $blog_id );
  545. $this->assertEquals( '1', $blog->spam );
  546. $this->assertEquals( $count, $test_action_counter );
  547. remove_action( 'make_spam_blog', array( $this, '_action_counter_cb' ), 10, 1 );
  548. add_action( 'archive_blog', array( $this, '_action_counter_cb' ), 10, 1 );
  549. $count++;
  550. $result = update_blog_status( $blog_id, 'archived', 1 );
  551. $this->assertEquals( 1, $result );
  552. $blog = get_blog_details( $blog_id );
  553. $this->assertEquals( '1', $blog->archived );
  554. $this->assertEquals( $count, $test_action_counter );
  555. // Same again
  556. $count++;
  557. $result = update_blog_status( $blog_id, 'archived', 1 );
  558. $this->assertEquals( 1, $result );
  559. $blog = get_blog_details( $blog_id );
  560. $this->assertEquals( '1', $blog->archived );
  561. $this->assertEquals( $count, $test_action_counter );
  562. remove_action( 'archive_blog', array( $this, '_action_counter_cb' ), 10, 1 );
  563. add_action( 'unarchive_blog', array( $this, '_action_counter_cb' ), 10, 1 );
  564. $count++;
  565. $result = update_blog_status( $blog_id, 'archived', 0 );
  566. $this->assertEquals( 0, $result );
  567. $blog = get_blog_details( $blog_id );
  568. $this->assertEquals( '0', $blog->archived );
  569. $this->assertEquals( $count, $test_action_counter );
  570. // Same again
  571. $result = update_blog_status( $blog_id, 'archived', 0 );
  572. $count++;
  573. $this->assertEquals( 0, $result );
  574. $blog = get_blog_details( $blog_id );
  575. $this->assertEquals( '0', $blog->archived );
  576. $this->assertEquals( $count, $test_action_counter );
  577. remove_action( 'unarchive_blog', array( $this, '_action_counter_cb' ), 10, 1 );
  578. add_action( 'make_delete_blog', array( $this, '_action_counter_cb' ), 10, 1 );
  579. $count++;
  580. $result = update_blog_status( $blog_id, 'deleted', 1 );
  581. $this->assertEquals( 1, $result );
  582. $blog = get_blog_details( $blog_id );
  583. $this->assertEquals( '1', $blog->deleted );
  584. $this->assertEquals( $count, $test_action_counter );
  585. // Same again
  586. $count++;
  587. $result = update_blog_status( $blog_id, 'deleted', 1 );
  588. $this->assertEquals( 1, $result );
  589. $blog = get_blog_details( $blog_id );
  590. $this->assertEquals( '1', $blog->deleted );
  591. $this->assertEquals( $count, $test_action_counter );
  592. remove_action( 'make_delete_blog', array( $this, '_action_counter_cb' ), 10, 1 );
  593. add_action( 'make_undelete_blog', array( $this, '_action_counter_cb' ), 10, 1 );
  594. $count++;
  595. $result = update_blog_status( $blog_id, 'deleted', 0 );
  596. $this->assertEquals( 0, $result );
  597. $blog = get_blog_details( $blog_id );
  598. $this->assertEquals( '0', $blog->deleted );
  599. $this->assertEquals( $count, $test_action_counter );
  600. // Same again
  601. $count++;
  602. $result = update_blog_status( $blog_id, 'deleted', 0 );
  603. $this->assertEquals( 0, $result );
  604. $blog = get_blog_details( $blog_id );
  605. $this->assertEquals( '0', $blog->deleted );
  606. $this->assertEquals( $count, $test_action_counter );
  607. remove_action( 'make_undelete_blog', array( $this, '_action_counter_cb' ), 10, 1 );
  608. add_action( 'mature_blog', array( $this, '_action_counter_cb' ), 10, 1 );
  609. $count++;
  610. $result = update_blog_status( $blog_id, 'mature', 1 );
  611. $this->assertEquals( 1, $result );
  612. $blog = get_blog_details( $blog_id );
  613. $this->assertEquals( '1', $blog->mature );
  614. $this->assertEquals( $count, $test_action_counter );
  615. // Same again
  616. $count++;
  617. $result = update_blog_status( $blog_id, 'mature', 1 );
  618. $this->assertEquals( 1, $result );
  619. $blog = get_blog_details( $blog_id );
  620. $this->assertEquals( '1', $blog->mature );
  621. $this->assertEquals( $count, $test_action_counter );
  622. remove_action( 'mature_blog', array( $this, '_action_counter_cb' ), 10, 1 );
  623. add_action( 'unmature_blog', array( $this, '_action_counter_cb' ), 10, 1 );
  624. $count++;
  625. $result = update_blog_status( $blog_id, 'mature', 0 );
  626. $this->assertEquals( 0, $result );
  627. $blog = get_blog_details( $blog_id );
  628. $this->assertEquals( '0', $blog->mature );
  629. $this->assertEquals( $count, $test_action_counter );
  630. // Same again
  631. $count++;
  632. $result = update_blog_status( $blog_id, 'mature', 0 );
  633. $this->assertEquals( 0, $result );
  634. $blog = get_blog_details( $blog_id );
  635. $this->assertEquals( '0', $blog->mature );
  636. $this->assertEquals( $count, $test_action_counter );
  637. remove_action( 'unmature_blog', array( $this, '_action_counter_cb' ), 10, 1 );
  638. add_action( 'update_blog_public', array( $this, '_action_counter_cb' ), 10, 1 );
  639. $count++;
  640. $result = update_blog_status( $blog_id, 'public', 0 );
  641. $this->assertEquals( 0, $result );
  642. $blog = get_blog_details( $blog_id );
  643. $this->assertEquals( '0', $blog->public );
  644. $this->assertEquals( $count, $test_action_counter );
  645. // Same again
  646. $count++;
  647. $result = update_blog_status( $blog_id, 'public', 0 );
  648. $this->assertEquals( 0, $result );
  649. $blog = get_blog_details( $blog_id );
  650. $this->assertEquals( '0', $blog->public );
  651. $this->assertEquals( $count, $test_action_counter );
  652. remove_action( 'update_blog_public', array( $this, '_action_counter_cb' ), 10, 1 );
  653. add_action( 'update_blog_public', array( $this, '_action_counter_cb' ), 10, 1 );
  654. $count++;
  655. $result = update_blog_status( $blog_id, 'public', 1 );
  656. $this->assertEquals( 1, $result );
  657. $blog = get_blog_details( $blog_id );
  658. $this->assertEquals( '1', $blog->public );
  659. $this->assertEquals( $count, $test_action_counter );
  660. // Same again
  661. $count++;
  662. $result = update_blog_status( $blog_id, 'public', 1 );
  663. $this->assertEquals( 1, $result );
  664. $blog = get_blog_details( $blog_id );
  665. $this->assertEquals( '1', $blog->public );
  666. $this->assertEquals( $count, $test_action_counter );
  667. remove_action( 'update_blog_public', array( $this, '_action_counter_cb' ), 10, 1 );
  668. // Updating a dummy field returns the value passed. Go fig.
  669. $result = update_blog_status( $blog_id, 'doesnotexist', 1 );
  670. $this->assertEquals( 1, $result );
  671. }
  672. function test_switch_restore_blog() {
  673. global $_wp_switched_stack, $wpdb;
  674. $this->assertEquals( array(), $_wp_switched_stack );
  675. $this->assertFalse( ms_is_switched() );
  676. $current_blog_id = get_current_blog_id();
  677. $this->assertInternalType( 'integer', $current_blog_id );
  678. wp_cache_set( 'switch-test', $current_blog_id, 'switch-test' );
  679. $this->assertEquals( $current_blog_id, wp_cache_get( 'switch-test', 'switch-test' ) );
  680. $user_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
  681. $blog_id = $this->factory->blog->create( array( 'user_id' => $user_id, 'path' => '/test_blogpath', 'title' => 'Test Title' ) );
  682. $cap_key = wp_get_current_user()->cap_key;
  683. switch_to_blog( $blog_id );
  684. $this->assertNotEquals( $cap_key, wp_get_current_user()->cap_key );
  685. $this->assertEquals( array( $current_blog_id ), $_wp_switched_stack );
  686. $this->assertTrue( ms_is_switched() );
  687. $this->assertEquals( $blog_id, $wpdb->blogid );
  688. $this->assertFalse( wp_cache_get( 'switch-test', 'switch-test' ) );
  689. wp_cache_set( 'switch-test', $blog_id, 'switch-test' );
  690. $this->assertEquals( $blog_id, wp_cache_get( 'switch-test', 'switch-test' ) );
  691. switch_to_blog( $blog_id );
  692. $this->assertEquals( array( $current_blog_id, $blog_id ), $_wp_switched_stack );
  693. $this->assertTrue( ms_is_switched() );
  694. $this->assertEquals( $blog_id, $wpdb->blogid );
  695. $this->assertEquals( $blog_id, wp_cache_get( 'switch-test', 'switch-test' ) );
  696. restore_current_blog();
  697. $this->assertEquals( array( $current_blog_id ), $_wp_switched_stack );
  698. $this->assertTrue( ms_is_switched() );
  699. $this->assertEquals( $blog_id, $wpdb->blogid );
  700. $this->assertEquals( $blog_id, wp_cache_get( 'switch-test', 'switch-test' ) );
  701. restore_current_blog();
  702. $this->assertEquals( $cap_key, wp_get_current_user()->cap_key );
  703. $this->assertEquals( $current_blog_id, get_current_blog_id() );
  704. $this->assertEquals( array(), $_wp_switched_stack );
  705. $this->assertFalse( ms_is_switched() );
  706. $this->assertEquals( $current_blog_id, wp_cache_get( 'switch-test', 'switch-test' ) );
  707. $this->assertFalse( restore_current_blog() );
  708. }
  709. function test_get_blog_post() {
  710. $user_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
  711. $blog_id = $this->factory->blog->create( array( 'user_id' => $user_id, 'path' => '/test_blogpath', 'title' => 'Test Title' ) );
  712. $current_blog_id = get_current_blog_id();
  713. $post_id = $this->factory->post->create();
  714. $this->assertInstanceOf( 'WP_Post', get_post( $post_id ) );
  715. switch_to_blog( $blog_id );
  716. $this->assertNull( get_post( $post_id ) );
  717. $post = get_blog_post( $current_blog_id, $post_id );
  718. $this->assertInstanceOf( 'WP_Post', $post );
  719. $this->assertEquals( $post_id, $post->ID );
  720. restore_current_blog();
  721. wp_update_post( array( 'ID' => $post_id, 'post_title' => 'A Different Title' ) );
  722. switch_to_blog( $blog_id );
  723. $post = get_blog_post( $current_blog_id, $post_id );
  724. // Make sure cache is good
  725. $this->assertEquals( 'A Different Title', $post->post_title );
  726. $post_id2 = $this->factory->post->create();
  727. // Test get_blog_post() with currently active blog ID.
  728. $post = get_blog_post( $blog_id, $post_id2 );
  729. $this->assertInstanceOf( 'WP_Post', $post );
  730. $this->assertEquals( $post_id2, $post->ID );
  731. restore_current_blog();
  732. }
  733. /**
  734. * @ticket 21570
  735. */
  736. function test_aggressiveness_of_is_email_address_unsafe() {
  737. update_site_option( 'banned_email_domains', array( 'bar.com', 'foo.co' ) );
  738. foreach ( array( 'test@bar.com', 'test@foo.bar.com', 'test@foo.co', 'test@subdomain.foo.co' ) as $email_address ) {
  739. $this->assertTrue( is_email_address_unsafe( $email_address ), "$email_address should be UNSAFE" );
  740. }
  741. foreach ( array( 'test@foobar.com', 'test@foo-bar.com', 'test@foo.com', 'test@subdomain.foo.com' ) as $email_address ) {
  742. $this->assertFalse( is_email_address_unsafe( $email_address ), "$email_address should be SAFE" );
  743. }
  744. }
  745. /**
  746. * @ticket 25046
  747. */
  748. function test_case_sensitivity_of_is_email_address_unsafe() {
  749. update_site_option( 'banned_email_domains', array( 'baR.com', 'Foo.co', 'barfoo.COM', 'BAZ.com' ) );
  750. foreach ( array( 'test@Bar.com', 'tEst@bar.com', 'test@barFoo.com', 'tEst@foo.bar.com', 'test@baz.Com' ) as $email_address ) {
  751. $this->assertTrue( is_email_address_unsafe( $email_address ), "$email_address should be UNSAFE" );
  752. }
  753. foreach ( array( 'test@Foobar.com', 'test@Foo-bar.com', 'tEst@foobar.com', 'test@Subdomain.Foo.com', 'test@fooBAz.com' ) as $email_address ) {
  754. $this->assertFalse( is_email_address_unsafe( $email_address ), "$email_address should be SAFE" );
  755. }
  756. }
  757. /**
  758. * @ticket 21552
  759. * @ticket 23418
  760. */
  761. function test_sanitize_ms_options() {
  762. update_site_option( 'illegal_names', array( '', 'Woo', '' ) );
  763. update_site_option( 'limited_email_domains', array( 'woo', '', 'boo.com', 'foo.net.biz..' ) );
  764. update_site_option( 'banned_email_domains', array( 'woo', '', 'boo.com', 'foo.net.biz..' ) );
  765. $this->assertEquals( array( 'Woo' ), get_site_option( 'illegal_names' ) );
  766. $this->assertEquals( array( 'woo', 'boo.com' ), get_site_option( 'limited_email_domains' ) );
  767. $this->assertEquals( array( 'woo', 'boo.com' ), get_site_option( 'banned_email_domains' ) );
  768. update_site_option( 'illegal_names', 'foo bar' );
  769. update_site_option( 'limited_email_domains', "foo\nbar" );
  770. update_site_option( 'banned_email_domains', "foo\nbar" );
  771. $this->assertEquals( array( 'foo', 'bar' ), get_site_option( 'illegal_names' ) );
  772. $this->assertEquals( array( 'foo', 'bar' ), get_site_option( 'limited_email_domains' ) );
  773. $this->assertEquals( array( 'foo', 'bar' ), get_site_option( 'banned_email_domains' ) );
  774. foreach ( array( 'illegal_names', 'limited_email_domains', 'banned_email_domains' ) as $option ) {
  775. update_site_option( $option, array() );
  776. $this->assertSame( '', get_site_option( $option ) );
  777. }
  778. }
  779. function _domain_exists_cb( $exists, $domain, $path, $site_id ) {
  780. if ( 'foo' == $domain && 'bar' == $path )
  781. return 1234;
  782. else
  783. return null;
  784. }
  785. function test_domain_exists() {
  786. $user_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
  787. $blog_id = $this->factory->blog->create( array( 'user_id' => $user_id, 'path' => '/testdomainexists', 'title' => 'Test Title' ) );
  788. $details = get_blog_details( $blog_id, false );
  789. $this->assertEquals( $blog_id, domain_exists( $details->domain, $details->path ) );
  790. $this->assertEquals( $blog_id, domain_exists( $details->domain, $details->path, $details->site_id ) );
  791. $this->assertEquals( null, domain_exists( $details->domain, $details->path, 999 ) );
  792. $this->assertEquals( null, domain_exists( 'foo', 'bar' ) );
  793. add_filter( 'domain_exists', array( $this, '_domain_exists_cb' ), 10, 4 );
  794. $this->assertEquals( 1234, domain_exists( 'foo', 'bar' ) );
  795. $this->assertEquals( null, domain_exists( 'foo', 'baz' ) );
  796. $this->assertEquals( null, domain_exists( 'bar', 'foo' ) );
  797. remove_filter( 'domain_exists', array( $this, '_domain_exists_cb' ), 10, 4 );
  798. $this->assertEquals( null, domain_exists( 'foo', 'bar' ) );
  799. wpmu_delete_blog( $blog_id );
  800. $this->assertEquals( $blog_id, domain_exists( $details->domain, $details->path ) );
  801. wpmu_delete_blog( $blog_id, true );
  802. $this->assertEquals( null, domain_exists( $details->domain, $details->path ) );
  803. }
  804. function test_get_blog_id_from_url() {
  805. $user_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
  806. $blog_id = $this->factory->blog->create( array( 'user_id' => $user_id, 'path' => '/testdomainexists', 'title' => 'Test Title' ) );
  807. $details = get_blog_details( $blog_id, false );
  808. $this->assertEquals( $blog_id, get_blog_id_from_url( $details->domain, $details->path ) );
  809. $key = md5( $details->domain . $details->path );
  810. $this->assertEquals( $blog_id, wp_cache_get( $key, 'blog-id-cache' ) );
  811. $this->assertEquals( 0, get_blog_id_from_url( $details->domain, 'foo' ) );
  812. wpmu_delete_blog( $blog_id );
  813. $this->assertEquals( $blog_id, get_blog_id_from_url( $details->domain, $details->path ) );
  814. wpmu_delete_blog( $blog_id, true );
  815. $this->assertEquals( false, wp_cache_get( $key, 'blog-id-cache' ) );
  816. $this->assertEquals( 0, get_blog_id_from_url( $details->domain, $details->path ) );
  817. }
  818. function test_is_main_site() {
  819. $this->assertTrue( is_main_site() );
  820. $this->assertTrue( is_main_site( get_current_blog_id() ) );
  821. $user_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
  822. $blog_id = $this->factory->blog->create( array( 'user_id' => $user_id ) );
  823. switch_to_blog( $blog_id );
  824. $this->assertFalse( is_main_site( $blog_id ) );
  825. $this->assertFalse( is_main_site( get_current_blog_id() ) );
  826. $this->assertFalse( is_main_site() );
  827. restore_current_blog();
  828. }
  829. function test_switch_upload_dir() {
  830. $this->assertTrue( is_main_site() );
  831. $site = get_current_site();
  832. $info = wp_upload_dir();
  833. $this->assertEquals( 'http://' . $site->domain . '/wp-content/uploads/' . gmstrftime('%Y/%m'), $info['url'] );
  834. $this->assertEquals( ABSPATH . 'wp-content/uploads/' . gmstrftime('%Y/%m'), $info['path'] );
  835. $this->assertEquals( gmstrftime('/%Y/%m'), $info['subdir'] );
  836. $this->assertEquals( '', $info['error'] );
  837. $user_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
  838. $blog_id = $this->factory->blog->create( array( 'user_id' => $user_id ) );
  839. switch_to_blog( $blog_id );
  840. $info = wp_upload_dir();
  841. $this->assertEquals( 'http://' . $site->domain . '/wp-content/uploads/sites/' . get_current_blog_id() . '/' . gmstrftime('%Y/%m'), $info['url'] );
  842. $this->assertEquals( ABSPATH . 'wp-content/uploads/sites/' . get_current_blog_id() . '/' . gmstrftime('%Y/%m'), $info['path'] );
  843. $this->assertEquals( gmstrftime('/%Y/%m'), $info['subdir'] );
  844. $this->assertEquals( '', $info['error'] );
  845. restore_current_blog();
  846. $info = wp_upload_dir();
  847. $this->assertEquals( 'http://' . $site->domain . '/wp-content/uploads/' . gmstrftime('%Y/%m'), $info['url'] );
  848. $this->assertEquals( ABSPATH . 'wp-content/uploads/' . gmstrftime('%Y/%m'), $info['path'] );
  849. $this->assertEquals( gmstrftime('/%Y/%m'), $info['subdir'] );
  850. $this->assertEquals( '', $info['error'] );
  851. update_site_option( 'ms_files_rewriting', 1 );
  852. ms_upload_constants();
  853. $user_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
  854. $blog_id2 = $this->factory->blog->create( array( 'user_id' => $user_id ) );
  855. $info = wp_upload_dir();
  856. $this->assertEquals( 'http://' . $site->domain . '/wp-content/uploads/' . gmstrftime('%Y/%m'), $info['url'] );
  857. $this->assertEquals( ABSPATH . 'wp-content/uploads/' . gmstrftime('%Y/%m'), $info['path'] );
  858. $this->assertEquals( gmstrftime('/%Y/%m'), $info['subdir'] );
  859. $this->assertEquals( '', $info['error'] );
  860. switch_to_blog( $blog_id2 );
  861. $info2 = wp_upload_dir();
  862. $this->assertNotEquals( $info, $info2 );
  863. $this->assertEquals( get_option( 'siteurl' ) . '/wp-content/blogs.dir/' . get_current_blog_id() . '/files/' . gmstrftime('%Y/%m'), $info2['url'] );
  864. $this->assertEquals( ABSPATH . 'wp-content/blogs.dir/' . get_current_blog_id() . '/files/' . gmstrftime('%Y/%m'), $info2['path'] );
  865. $this->assertEquals( gmstrftime('/%Y/%m'), $info2['subdir'] );
  866. $this->assertEquals( '', $info2['error'] );
  867. restore_current_blog();
  868. update_site_option( 'ms_files_rewriting', 0 );
  869. }
  870. /**
  871. * @ticket 23192
  872. */
  873. function test_is_user_spammy() {
  874. $user_id = $this->factory->user->create( array(
  875. 'role' => 'author',
  876. 'user_login' => 'testuser1',
  877. ) );
  878. $spam_username = (string) $user_id;
  879. $spam_user_id = $this->factory->user->create( array(
  880. 'role' => 'author',
  881. 'user_login' => $spam_username,
  882. ) );
  883. update_user_status( $spam_user_id, 'spam', '1' );
  884. $this->assertTrue( is_user_spammy( $spam_username ) );
  885. $this->assertFalse( is_user_spammy( 'testuser1' ) );
  886. }
  887. /**
  888. * @ticket 14511
  889. */
  890. function test_wp_get_sites() {
  891. $this->factory->blog->create_many( 2, array( 'site_id' => 2, 'meta' => array( 'public' => 1 ) ) );
  892. $this->factory->blog->create_many( 3, array( 'site_id' => 3, 'meta' => array( 'public' => 0 ) ) );
  893. // Expect no sites when passed an invalid network_id
  894. $this->assertCount( 0, wp_get_sites( array( 'network_id' => 0 ) ) );
  895. $this->assertCount( 0, wp_get_sites( array( 'network_id' => 4 ) ) );
  896. // Expect 1 site when no network_id is specified - defaults to current network.
  897. $this->assertCount( 1, wp_get_sites() );
  898. // Expect 6 sites when network_id = null.
  899. $this->assertCount( 6, wp_get_sites( array( 'network_id' => null ) ) );
  900. // Expect 1 site with a network_id of 1, 2 for network_id 2, 3 for 3
  901. $this->assertCount( 1, wp_get_sites( array( 'network_id' => 1 ) ) );
  902. $this->assertCount( 2, wp_get_sites( array( 'network_id' => 2 ) ) );
  903. $this->assertCount( 3, wp_get_sites( array( 'network_id' => 3 ) ) );
  904. // Expect 6 sites when public is null (across all networks)
  905. $this->assertCount( 6, wp_get_sites( array( 'public' => null, 'network_id' => null ) ) );
  906. // Expect 3 sites when public is 1
  907. $this->assertCount( 3, wp_get_sites( array( 'public' => 1, 'network_id' => null ) ) );
  908. // Expect 2 sites when public is 1 and network_id is 2
  909. $this->assertCount( 2, wp_get_sites( array( 'network_id' => 2, 'public' => 1 ) ) );
  910. // Expect no sites when public is set to 0 and network_id is not 3
  911. $this->assertCount( 0, wp_get_sites( array( 'network_id' => 1, 'public' => 0 ) ) );
  912. // Test public + network_id = 3
  913. $this->assertCount( 0, wp_get_sites( array( 'network_id' => 3, 'public' => 1 ) ) );
  914. $this->assertCount( 3, wp_get_sites( array( 'network_id' => 3, 'public' => 0 ) ) );
  915. }
  916. /**
  917. * @ticket 14511
  918. */
  919. function test_wp_get_sites_limit_offset() {
  920. // Create 4 more sites (in addition to the default one)
  921. $this->factory->blog->create_many( 4, array( 'meta' => array( 'public' => 1 ) ) );
  922. // Expect all 5 sites when no limit/offset is specified
  923. $this->assertCount( 5, wp_get_sites() );
  924. // Expect first 2 sites when using limit
  925. $this->assertCount( 2, wp_get_sites( array( 'limit' => 2 ) ) );
  926. // Expect only the last 3 sites when using offset of 2 (limit will default to 100)
  927. $this->assertCount( 3, wp_get_sites( array( 'offset' => 2 ) ) );
  928. // Expect only the last 1 site when using offset of 4 and limit of 2
  929. $this->assertCount( 1, wp_get_sites( array( 'limit' => 2, 'offset' => 4 ) ) );
  930. // Expect 0 sites when using an offset larger than the number of sites
  931. $this->assertCount( 0, wp_get_sites( array( 'offset' => 20 ) ) );
  932. }
  933. }
  934. endif;