Make WordPress Core


Ignore:
Timestamp:
08/01/2018 01:05:44 PM (6 years ago)
Author:
flixos90
Message:

Multisite: Complete the new CRUD API for managing sites.

New functions wp_insert_site( $data ), wp_update_site( $id, $data ) and wp_delete_site( $id ) are introduced to manage site rows in the wp_blogs table, forming the new CRUD API together with the existing get_site() / get_sites(). The new API provides various benefits over the previously existing API, fixing several cache invalidation issues and being hook-driven so that normalization and validation of the passed data can be fully customized.

New hooks introduced as part of this are the actions wp_insert_site, wp_update_site, wp_delete_site, wp_validate_site_data and the filter wp_normalize_site_data.

At this point, wp_insert_site() does not handle setting up the site's database tables, and wp_delete_site() does not handle dropping the site's database tables, so the two can not yet be used directly as full replacements of wpmu_create_blog() and wpmu_delete_blog(). Managing the site's database tables will be added via hooks as part of the follow-up ticket #41333.

The existing functions wpmu_create_blog(), update_blog_details(), and wpmu_delete_blog() make use of the respective new counterpart and will be obsolete once #41333 has been completed.

Props flixos90, jeremyfelt, spacedmonkey.
Fixes #40364.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/multisite/site.php

    r42343 r43548  
    1111    class Tests_Multisite_Site extends WP_UnitTestCase {
    1212        protected $suppress = false;
     13        protected $site_status_hooks = array();
    1314        protected static $network_ids;
    1415        protected static $site_ids;
     
    447448            $this->assertEquals( 1, $test_action_counter );
    448449
    449             // The action should fire if the status of 'spam' stays the same.
     450            // The action should not fire if the status of 'spam' stays the same.
    450451            update_blog_status( $blog_id, 'spam', 0 );
    451452            $blog = get_site( $blog_id );
    452453
    453454            $this->assertEquals( '0', $blog->spam );
    454             $this->assertEquals( 2, $test_action_counter );
     455            $this->assertEquals( 1, $test_action_counter );
    455456
    456457            remove_action( 'make_ham_blog', array( $this, '_action_counter_cb' ), 10 );
     
    470471            $this->assertEquals( 1, $test_action_counter );
    471472
    472             // The action should fire if the status of 'spam' stays the same.
     473            // The action should not fire if the status of 'spam' stays the same.
    473474            update_blog_status( $blog_id, 'spam', 1 );
    474475            $blog = get_site( $blog_id );
    475476
    476477            $this->assertEquals( '1', $blog->spam );
    477             $this->assertEquals( 2, $test_action_counter );
     478            $this->assertEquals( 1, $test_action_counter );
    478479
    479480            remove_action( 'make_spam_blog', array( $this, '_action_counter_cb' ), 10 );
     
    493494            $this->assertEquals( 1, $test_action_counter );
    494495
    495             // The action should fire if the status of 'archived' stays the same.
     496            // The action should not fire if the status of 'archived' stays the same.
    496497            update_blog_status( $blog_id, 'archived', 1 );
    497498            $blog = get_site( $blog_id );
    498499
    499500            $this->assertEquals( '1', $blog->archived );
    500             $this->assertEquals( 2, $test_action_counter );
     501            $this->assertEquals( 1, $test_action_counter );
    501502
    502503            remove_action( 'archive_blog', array( $this, '_action_counter_cb' ), 10 );
     
    517518            $this->assertEquals( 1, $test_action_counter );
    518519
    519             // The action should fire if the status of 'archived' stays the same.
     520            // The action should not fire if the status of 'archived' stays the same.
    520521            update_blog_status( $blog_id, 'archived', 0 );
    521522            $blog = get_site( $blog_id );
    522523            $this->assertEquals( '0', $blog->archived );
    523             $this->assertEquals( 2, $test_action_counter );
     524            $this->assertEquals( 1, $test_action_counter );
    524525
    525526            remove_action( 'unarchive_blog', array( $this, '_action_counter_cb' ), 10 );
     
    539540            $this->assertEquals( 1, $test_action_counter );
    540541
    541             // The action should fire if the status of 'deleted' stays the same.
     542            // The action should not fire if the status of 'deleted' stays the same.
    542543            update_blog_status( $blog_id, 'deleted', 1 );
    543544            $blog = get_site( $blog_id );
    544545
    545546            $this->assertEquals( '1', $blog->deleted );
    546             $this->assertEquals( 2, $test_action_counter );
     547            $this->assertEquals( 1, $test_action_counter );
    547548
    548549            remove_action( 'make_delete_blog', array( $this, '_action_counter_cb' ), 10 );
     
    563564            $this->assertEquals( 1, $test_action_counter );
    564565
    565             // The action should fire if the status of 'deleted' stays the same.
     566            // The action should not fire if the status of 'deleted' stays the same.
    566567            update_blog_status( $blog_id, 'deleted', 0 );
    567568            $blog = get_site( $blog_id );
    568569
    569570            $this->assertEquals( '0', $blog->deleted );
    570             $this->assertEquals( 2, $test_action_counter );
     571            $this->assertEquals( 1, $test_action_counter );
    571572
    572573            remove_action( 'make_undelete_blog', array( $this, '_action_counter_cb' ), 10 );
     
    586587            $this->assertEquals( 1, $test_action_counter );
    587588
    588             // The action should fire if the status of 'mature' stays the same.
     589            // The action should not fire if the status of 'mature' stays the same.
    589590            update_blog_status( $blog_id, 'mature', 1 );
    590591            $blog = get_site( $blog_id );
    591592
    592593            $this->assertEquals( '1', $blog->mature );
    593             $this->assertEquals( 2, $test_action_counter );
     594            $this->assertEquals( 1, $test_action_counter );
    594595
    595596            remove_action( 'mature_blog', array( $this, '_action_counter_cb' ), 10 );
     
    610611            $this->assertEquals( 1, $test_action_counter );
    611612
    612             // The action should fire if the status of 'mature' stays the same.
     613            // The action should not fire if the status of 'mature' stays the same.
    613614            update_blog_status( $blog_id, 'mature', 0 );
    614615            $blog = get_site( $blog_id );
    615616
    616617            $this->assertEquals( '0', $blog->mature );
    617             $this->assertEquals( 2, $test_action_counter );
     618            $this->assertEquals( 1, $test_action_counter );
    618619
    619620            remove_action( 'unmature_blog', array( $this, '_action_counter_cb' ), 10 );
     
    633634            $this->assertEquals( 1, $test_action_counter );
    634635
    635             // The action should fire if the status of 'mature' stays the same.
     636            // The action should not fire if the status of 'mature' stays the same.
    636637            update_blog_status( $blog_id, 'public', 0 );
    637638            $blog = get_site( $blog_id );
    638639
    639640            $this->assertEquals( '0', $blog->public );
    640             $this->assertEquals( 2, $test_action_counter );
     641            $this->assertEquals( 1, $test_action_counter );
    641642
    642643            remove_action( 'update_blog_public', array( $this, '_action_counter_cb' ), 10 );
     
    12601261            );
    12611262        }
     1263
     1264        /**
     1265         * @ticket 40364
     1266         * @dataProvider data_wp_insert_site
     1267         */
     1268        public function test_wp_insert_site( $site_data, $expected_data ) {
     1269            $site_id = wp_insert_site( $site_data );
     1270
     1271            $this->assertInternalType( 'integer', $site_id );
     1272
     1273            $site = get_site( $site_id );
     1274            foreach ( $expected_data as $key => $value ) {
     1275                $this->assertEquals( $value, $site->$key );
     1276            }
     1277        }
     1278
     1279        public function data_wp_insert_site() {
     1280            return array(
     1281                array(
     1282                    array(
     1283                        'domain' => 'example.com',
     1284                    ),
     1285                    array(
     1286                        'domain'     => 'example.com',
     1287                        'path'       => '/',
     1288                        'network_id' => 1,
     1289                        'public'     => 1,
     1290                        'archived'   => 0,
     1291                        'mature'     => 0,
     1292                        'spam'       => 0,
     1293                        'deleted'    => 0,
     1294                        'lang_id'    => 0,
     1295                    ),
     1296                ),
     1297                array(
     1298                    array(
     1299                        'domain'     => 'example.com',
     1300                        'path'       => '/foo',
     1301                        'network_id' => 2,
     1302                    ),
     1303                    array(
     1304                        'domain'     => 'example.com',
     1305                        'path'       => '/foo/',
     1306                        'network_id' => 2,
     1307                    ),
     1308                ),
     1309                array(
     1310                    array(
     1311                        'domain'  => 'example.com',
     1312                        'path'    => '/bar/',
     1313                        'site_id' => 2,
     1314                    ),
     1315                    array(
     1316                        'domain'     => 'example.com',
     1317                        'path'       => '/bar/',
     1318                        'network_id' => 2,
     1319                    ),
     1320                ),
     1321                array(
     1322                    array(
     1323                        'domain'     => 'example.com',
     1324                        'path'       => '/bar/',
     1325                        'site_id'    => 2,
     1326                        'network_id' => 3,
     1327                    ),
     1328                    array(
     1329                        'domain'     => 'example.com',
     1330                        'path'       => '/bar/',
     1331                        'network_id' => 3,
     1332                    ),
     1333                ),
     1334                array(
     1335                    array(
     1336                        'domain'   => 'example.com',
     1337                        'path'     => 'foobar',
     1338                        'public'   => 0,
     1339                        'archived' => 1,
     1340                        'mature'   => 1,
     1341                        'spam'     => 1,
     1342                        'deleted'  => 1,
     1343                        'lang_id'  => 1,
     1344                    ),
     1345                    array(
     1346                        'domain'   => 'example.com',
     1347                        'path'     => '/foobar/',
     1348                        'public'   => 0,
     1349                        'archived' => 1,
     1350                        'mature'   => 1,
     1351                        'spam'     => 1,
     1352                        'deleted'  => 1,
     1353                        'lang_id'  => 1,
     1354                    ),
     1355                ),
     1356            );
     1357        }
     1358
     1359        /**
     1360         * @ticket 40364
     1361         */
     1362        public function test_wp_insert_site_empty_domain() {
     1363            $site_id = wp_insert_site( array( 'public' => 0 ) );
     1364
     1365            $this->assertWPError( $site_id );
     1366            $this->assertSame( 'site_empty_domain', $site_id->get_error_code() );
     1367        }
     1368
     1369        /**
     1370         * @ticket 40364
     1371         * @dataProvider data_wp_update_site
     1372         */
     1373        public function test_wp_update_site( $site_data, $expected_data ) {
     1374            $site_id = self::factory()->blog->create();
     1375
     1376            $old_site = get_site( $site_id );
     1377
     1378            $result = wp_update_site( $site_id, $site_data );
     1379
     1380            $this->assertSame( $site_id, $result );
     1381
     1382            $new_site = get_site( $site_id );
     1383            foreach ( $new_site->to_array() as $key => $value ) {
     1384                if ( isset( $expected_data[ $key ] ) ) {
     1385                    $this->assertEquals( $expected_data[ $key ], $value );
     1386                } elseif ( 'last_updated' === $key ) {
     1387                    $this->assertTrue( $old_site->last_updated <= $value );
     1388                } else {
     1389                    $this->assertEquals( $old_site->$key, $value );
     1390                }
     1391            }
     1392        }
     1393
     1394        public function data_wp_update_site() {
     1395            return array(
     1396                array(
     1397                    array(
     1398                        'domain'     => 'example.com',
     1399                        'network_id' => 2,
     1400                    ),
     1401                    array(
     1402                        'domain'  => 'example.com',
     1403                        'site_id' => 2,
     1404                    ),
     1405                ),
     1406                array(
     1407                    array(
     1408                        'path' => 'foo',
     1409                    ),
     1410                    array(
     1411                        'path' => '/foo/',
     1412                    ),
     1413                ),
     1414                array(
     1415                    array(
     1416                        'public'   => 0,
     1417                        'archived' => 1,
     1418                        'mature'   => 1,
     1419                        'spam'     => 1,
     1420                        'deleted'  => 1,
     1421                        'lang_id'  => 1,
     1422                    ),
     1423                    array(
     1424                        'public'   => 0,
     1425                        'archived' => 1,
     1426                        'mature'   => 1,
     1427                        'spam'     => 1,
     1428                        'deleted'  => 1,
     1429                        'lang_id'  => 1,
     1430                    ),
     1431                ),
     1432            );
     1433        }
     1434
     1435        /**
     1436         * @ticket 40364
     1437         */
     1438        public function test_wp_update_site_empty_domain() {
     1439            $site_id = self::factory()->blog->create();
     1440
     1441            $result = wp_update_site( $site_id, array( 'domain' => '' ) );
     1442
     1443            $this->assertWPError( $result );
     1444            $this->assertSame( 'site_empty_domain', $result->get_error_code() );
     1445        }
     1446
     1447        /**
     1448         * @ticket 40364
     1449         */
     1450        public function test_wp_update_site_invalid_id() {
     1451            $result = wp_update_site( 444444, array( 'domain' => 'example.com' ) );
     1452
     1453            $this->assertWPError( $result );
     1454            $this->assertSame( 'site_not_exist', $result->get_error_code() );
     1455        }
     1456
     1457        /**
     1458         * @ticket 40364
     1459         */
     1460        public function test_wp_update_site_cleans_cache() {
     1461            $site_id = self::factory()->blog->create();
     1462            $site1 = get_site( $site_id );
     1463
     1464            $result = wp_update_site( $site_id, array( 'public' => 0 ) );
     1465            $site2 = get_site( $site_id );
     1466
     1467            $result = wp_update_site( $site_id, array( 'public' => 1 ) );
     1468            $site3 = get_site( $site_id );
     1469
     1470            $this->assertEquals( 1, $site1->public );
     1471            $this->assertEquals( 0, $site2->public );
     1472            $this->assertEquals( 1, $site3->public );
     1473        }
     1474
     1475        /**
     1476         * @ticket 40364
     1477         */
     1478        public function test_wp_delete_site() {
     1479            $site_id = self::factory()->blog->create();
     1480
     1481            $site = get_site( $site_id );
     1482
     1483            $result = wp_delete_site( $site_id );
     1484
     1485            $this->assertInstanceOf( 'WP_Site', $result );
     1486            $this->assertEquals( $result->to_array(), $site->to_array() );
     1487        }
     1488
     1489        /**
     1490         * @ticket 40364
     1491         */
     1492        public function test_wp_delete_site_invalid_id() {
     1493            $result = wp_delete_site( 444444 );
     1494
     1495            $this->assertWPError( $result );
     1496            $this->assertSame( 'site_not_exist', $result->get_error_code() );
     1497        }
     1498
     1499        /**
     1500         * @ticket 40364
     1501         * @dataProvider data_wp_normalize_site_data
     1502         */
     1503        public function test_wp_normalize_site_data( $data, $expected ) {
     1504            $result = wp_normalize_site_data( $data );
     1505
     1506            $this->assertEqualSetsWithIndex( $expected, $result );
     1507        }
     1508
     1509        public function data_wp_normalize_site_data() {
     1510            return array(
     1511                array(
     1512                    array(
     1513                        'network_id' => '4',
     1514                    ),
     1515                    array(
     1516                        'network_id' => 4,
     1517                    ),
     1518                ),
     1519                array(
     1520                    array(
     1521                        'domain' => 'invalid domain .com',
     1522                        'path'   => 'foo',
     1523                    ),
     1524                    array(
     1525                        'domain' => 'invaliddomain.com',
     1526                        'path'   => '/foo/',
     1527                    ),
     1528                ),
     1529                array(
     1530                    array(
     1531                        'domain' => '<yet>/another-invalid-domain.com',
     1532                    ),
     1533                    array(
     1534                        'domain' => 'another-invalid-domain.com',
     1535                    ),
     1536                ),
     1537                array(
     1538                    array(
     1539                        'path' => '',
     1540                    ),
     1541                    array(
     1542                        'path' => '/',
     1543                    ),
     1544                ),
     1545                array(
     1546                    array(
     1547                        'public'   => '0',
     1548                        'archived' => '1',
     1549                        'mature'   => '1',
     1550                        'spam'     => true,
     1551                        'deleted'  => true,
     1552                    ),
     1553                    array(
     1554                        'public'   => 0,
     1555                        'archived' => 1,
     1556                        'mature'   => 1,
     1557                        'spam'     => 1,
     1558                        'deleted'  => 1,
     1559                    ),
     1560                ),
     1561                array(
     1562                    array(
     1563                        'registered'   => '',
     1564                        'last_updated' => '',
     1565                    ),
     1566                    array(),
     1567                ),
     1568                array(
     1569                    array(
     1570                        'registered'   => '0000-00-00 00:00:00',
     1571                        'last_updated' => '0000-00-00 00:00:00',
     1572                    ),
     1573                    array(),
     1574                ),
     1575            );
     1576        }
     1577
     1578        /**
     1579         * @ticket 40364
     1580         * @dataProvider data_wp_validate_site_data
     1581         */
     1582        public function test_wp_validate_site_data( $data, $expected_errors ) {
     1583            $result = new WP_Error();
     1584            wp_validate_site_data( $result, $data );
     1585
     1586            if ( empty( $expected_errors ) ) {
     1587                $this->assertEmpty( $result->errors );
     1588            } else {
     1589                $this->assertEqualSets( $expected_errors, array_keys( $result->errors ) );
     1590            }
     1591        }
     1592
     1593        public function data_wp_validate_site_data() {
     1594            $date = current_time( 'mysql', true );
     1595
     1596            return array(
     1597                array(
     1598                    array(
     1599                        'domain'       => 'example-site.com',
     1600                        'path'         => '/',
     1601                        'network_id'   => 1,
     1602                        'registered'   => $date,
     1603                        'last_updated' => $date,
     1604                    ),
     1605                    array(),
     1606                ),
     1607                array(
     1608                    array(
     1609                        'path'         => '/',
     1610                        'network_id'   => 1,
     1611                        'registered'   => $date,
     1612                        'last_updated' => $date,
     1613                    ),
     1614                    array( 'site_empty_domain' ),
     1615                ),
     1616                array(
     1617                    array(
     1618                        'domain'       => 'example-site.com',
     1619                        'network_id'   => 1,
     1620                        'registered'   => $date,
     1621                        'last_updated' => $date,
     1622                    ),
     1623                    array( 'site_empty_path' ),
     1624                ),
     1625                array(
     1626                    array(
     1627                        'domain'       => 'example-site.com',
     1628                        'path'         => '/',
     1629                        'registered'   => $date,
     1630                        'last_updated' => $date,
     1631                    ),
     1632                    array( 'site_empty_network_id' ),
     1633                ),
     1634                array(
     1635                    array(
     1636                        'domain'       => get_site()->domain,
     1637                        'path'         => get_site()->path,
     1638                        'network_id'   => get_site()->network_id,
     1639                        'registered'   => $date,
     1640                        'last_updated' => $date,
     1641                    ),
     1642                    array( 'site_taken' ),
     1643                ),
     1644                array(
     1645                    array(
     1646                        'domain'       => 'valid-domain.com',
     1647                        'path'         => '/valid-path/',
     1648                        'network_id'   => 1,
     1649                        'registered'   => '',
     1650                        'last_updated' => $date,
     1651                    ),
     1652                    array( 'site_empty_registered' ),
     1653                ),
     1654                array(
     1655                    array(
     1656                        'domain'       => 'valid-domain.com',
     1657                        'path'         => '/valid-path/',
     1658                        'network_id'   => 1,
     1659                        'registered'   => $date,
     1660                        'last_updated' => '',
     1661                    ),
     1662                    array( 'site_empty_last_updated' ),
     1663                ),
     1664                array(
     1665                    array(
     1666                        'domain'       => 'valid-domain.com',
     1667                        'path'         => '/valid-path/',
     1668                        'network_id'   => 1,
     1669                        'registered'   => '2000-13-32 25:25:61',
     1670                        'last_updated' => $date,
     1671                    ),
     1672                    array( 'site_invalid_registered' ),
     1673                ),
     1674                array(
     1675                    array(
     1676                        'domain'       => 'valid-domain.com',
     1677                        'path'         => '/valid-path/',
     1678                        'network_id'   => 1,
     1679                        'registered'   => $date,
     1680                        'last_updated' => '2000-13-32 25:25:61',
     1681                    ),
     1682                    array( 'site_invalid_last_updated' ),
     1683                ),
     1684                array(
     1685                    array(
     1686                        'domain'       => 'valid-domain.com',
     1687                        'path'         => '/valid-path/',
     1688                        'network_id'   => 1,
     1689                        'registered'   => '0000-00-00 00:00:00',
     1690                        'last_updated' => $date,
     1691                    ),
     1692                    array(),
     1693                ),
     1694                array(
     1695                    array(
     1696                        'domain'       => 'valid-domain.com',
     1697                        'path'         => '/valid-path/',
     1698                        'network_id'   => 1,
     1699                        'registered'   => $date,
     1700                        'last_updated' => '0000-00-00 00:00:00',
     1701                    ),
     1702                    array(),
     1703                ),
     1704            );
     1705        }
     1706
     1707        /**
     1708         * @ticket 40364
     1709         */
     1710        public function test_site_dates_are_gmt() {
     1711            $first_date = current_time( 'mysql', true );
     1712            $site_id    = wp_insert_site( array(
     1713                'domain'     => 'valid-domain.com',
     1714                'path'       => '/valid-path/',
     1715                'network_id' => 1,
     1716            ) );
     1717            $this->assertInternalType( 'integer', $site_id );
     1718
     1719            $site = get_site( $site_id );
     1720            $this->assertSame( $first_date, $site->registered );
     1721            $this->assertSame( $first_date, $site->last_updated );
     1722
     1723            $second_date = current_time( 'mysql', true );
     1724            $site_id     = wp_update_site( $site_id, array() );
     1725            $this->assertInternalType( 'integer', $site_id );
     1726
     1727            $site = get_site( $site_id );
     1728            $this->assertSame( $first_date, $site->registered );
     1729            $this->assertSame( $second_date, $site->last_updated );
     1730        }
     1731
     1732        /**
     1733         * @ticket 40364
     1734         */
     1735        public function test_wp_delete_site_cleans_cache() {
     1736            $site_id = self::factory()->blog->create();
     1737
     1738            get_site( $site_id );
     1739
     1740            wp_delete_site( $site_id );
     1741
     1742            $this->assertNull( get_site( $site_id ) );
     1743        }
     1744
     1745        /**
     1746         * @ticket 40364
     1747         */
     1748        public function test_wp_update_site_cleans_old_cache_on_domain_change() {
     1749            $old_domain = 'old.wordpress.org';
     1750            $new_domain = 'new.wordpress.org';
     1751
     1752            $site = self::factory()->blog->create_and_get( array(
     1753                'domain' => $old_domain,
     1754                'path'   => '/',
     1755            ) );
     1756
     1757            // Populate the caches.
     1758            get_blog_details( array(
     1759                'domain' => $old_domain,
     1760                'path'   => '/',
     1761            ) );
     1762            get_blog_id_from_url( $old_domain, '/' );
     1763            get_blog_details( array(
     1764                'domain' => $new_domain,
     1765                'path'   => '/',
     1766            ) );
     1767            get_blog_id_from_url( $new_domain, '/' );
     1768
     1769            wp_update_site( $site->id, array(
     1770                'domain' => $new_domain,
     1771            ) );
     1772
     1773            $domain_path_key_old = md5( $old_domain . '/' );
     1774            $domain_path_key_new = md5( $new_domain . '/' );
     1775
     1776            // Ensure all respective cache values are empty.
     1777            $result = array(
     1778                wp_cache_get( $domain_path_key_old, 'blog-lookup' ),
     1779                wp_cache_get( $domain_path_key_old, 'blog-id-cache' ),
     1780                wp_cache_get( 'current_blog_' . $old_domain, 'site-options' ),
     1781                wp_cache_get( 'current_blog_' . $old_domain . '/', 'site-options' ),
     1782                wp_cache_get( $domain_path_key_new, 'blog-lookup' ),
     1783                wp_cache_get( $domain_path_key_new, 'blog-id-cache' ),
     1784                wp_cache_get( 'current_blog_' . $new_domain, 'site-options' ),
     1785                wp_cache_get( 'current_blog_' . $new_domain . '/', 'site-options' ),
     1786            );
     1787
     1788            $this->assertEmpty( array_filter( $result ) );
     1789        }
     1790
     1791        /**
     1792         * @ticket 40364
     1793         */
     1794        public function test_wp_update_site_cleans_old_cache_on_path_change() {
     1795            $old_path = '/foo/';
     1796            $new_path = '/bar/';
     1797
     1798            $site = self::factory()->blog->create_and_get( array(
     1799                'domain' => 'test.wordpress.org',
     1800                'path'   => $old_path,
     1801            ) );
     1802
     1803            // Populate the caches.
     1804            get_blog_details( array(
     1805                'domain' => 'test.wordpress.org',
     1806                'path'   => $old_path,
     1807            ) );
     1808            get_blog_id_from_url( 'test.wordpress.org', $old_path );
     1809            get_blog_details( array(
     1810                'domain' => 'test.wordpress.org',
     1811                'path'   => $new_path,
     1812            ) );
     1813            get_blog_id_from_url( 'test.wordpress.org', $new_path );
     1814
     1815            wp_update_site( $site->id, array(
     1816                'path' => $new_path,
     1817            ) );
     1818
     1819            $domain_path_key_old = md5( 'test.wordpress.org' . $old_path );
     1820            $domain_path_key_new = md5( 'test.wordpress.org' . $new_path );
     1821
     1822            // Ensure all respective cache values are empty.
     1823            $result = array(
     1824                wp_cache_get( $domain_path_key_old, 'blog-lookup' ),
     1825                wp_cache_get( $domain_path_key_old, 'blog-id-cache' ),
     1826                wp_cache_get( 'current_blog_test.wordpress.org' . $old_path, 'site-options' ),
     1827                wp_cache_get( $domain_path_key_new, 'blog-lookup' ),
     1828                wp_cache_get( $domain_path_key_new, 'blog-id-cache' ),
     1829                wp_cache_get( 'current_blog_test.wordpress.org' . $new_path, 'site-options' ),
     1830            );
     1831
     1832            $this->assertEmpty( array_filter( $result ) );
     1833        }
     1834
     1835        /**
     1836         * @ticket 40364
     1837         * @dataProvider data_site_status_hook_triggers
     1838         */
     1839        public function test_site_status_hook_triggers( $insert_site_data, $expected_insert_hooks, $update_site_data, $expected_update_hooks ) {
     1840            // First: Insert a site.
     1841            $this->listen_to_site_status_hooks();
     1842
     1843            $site_data = array_merge( array(
     1844                'domain' => 'example-site.com',
     1845                'path'   => '/',
     1846            ), $insert_site_data );
     1847
     1848            $site_id = wp_insert_site( $site_data );
     1849
     1850            $insert_expected = array_fill_keys( $expected_insert_hooks, $site_id );
     1851            $insert_result   = $this->get_listen_to_site_status_hooks_result();
     1852
     1853            // Second: Update that site.
     1854            $this->listen_to_site_status_hooks();
     1855
     1856            wp_update_site( $site_id, $update_site_data );
     1857
     1858            $update_expected = array_fill_keys( $expected_update_hooks, $site_id );
     1859            $update_result   = $this->get_listen_to_site_status_hooks_result();
     1860
     1861            // Check both insert and update results.
     1862            $this->assertEqualSetsWithIndex( $insert_expected, $insert_result );
     1863            $this->assertEqualSetsWithIndex( $update_expected, $update_result );
     1864        }
     1865
     1866        public function data_site_status_hook_triggers() {
     1867            return array(
     1868                array(
     1869                    array(
     1870                        'public'   => 1,
     1871                        'archived' => 1,
     1872                        'mature'   => 1,
     1873                        'spam'     => 1,
     1874                        'deleted'  => 1,
     1875                    ),
     1876                    array(
     1877                        'archive_blog',
     1878                        'mature_blog',
     1879                        'make_spam_blog',
     1880                        'make_delete_blog',
     1881                    ),
     1882                    array(
     1883                        'public'   => 0,
     1884                        'archived' => 0,
     1885                        'mature'   => 0,
     1886                        'spam'     => 0,
     1887                        'deleted'  => 0,
     1888                    ),
     1889                    array(
     1890                        'update_blog_public',
     1891                        'unarchive_blog',
     1892                        'unmature_blog',
     1893                        'make_ham_blog',
     1894                        'make_undelete_blog',
     1895                    ),
     1896                ),
     1897                array(
     1898                    array(
     1899                        'public'   => 0,
     1900                        'archived' => 0,
     1901                        'mature'   => 0,
     1902                        'spam'     => 0,
     1903                        'deleted'  => 0,
     1904                    ),
     1905                    array(
     1906                        'update_blog_public',
     1907                    ),
     1908                    array(
     1909                        'public'   => 1,
     1910                        'archived' => 1,
     1911                        'mature'   => 1,
     1912                        'spam'     => 1,
     1913                        'deleted'  => 1,
     1914                    ),
     1915                    array(
     1916                        'update_blog_public',
     1917                        'archive_blog',
     1918                        'mature_blog',
     1919                        'make_spam_blog',
     1920                        'make_delete_blog',
     1921                    ),
     1922                ),
     1923                array(
     1924                    array(
     1925                        'public'   => 0,
     1926                        'archived' => 0,
     1927                        'mature'   => 1,
     1928                        'spam'     => 1,
     1929                        'deleted'  => 1,
     1930                    ),
     1931                    array(
     1932                        'update_blog_public',
     1933                        'mature_blog',
     1934                        'make_spam_blog',
     1935                        'make_delete_blog',
     1936                    ),
     1937                    array(
     1938                        'public'   => 0,
     1939                        'archived' => 1,
     1940                        'mature'   => 1,
     1941                        'spam'     => 1,
     1942                        'deleted'  => 0,
     1943                    ),
     1944                    array(
     1945                        'archive_blog',
     1946                        'make_undelete_blog',
     1947                    ),
     1948                ),
     1949            );
     1950        }
     1951
     1952        private function listen_to_site_status_hooks() {
     1953            $this->site_status_hooks = array();
     1954
     1955            $hooknames = array(
     1956                'make_spam_blog',
     1957                'make_ham_blog',
     1958                'mature_blog',
     1959                'unmature_blog',
     1960                'archive_blog',
     1961                'unarchive_blog',
     1962                'make_delete_blog',
     1963                'make_undelete_blog',
     1964                'update_blog_public',
     1965            );
     1966
     1967            foreach ( $hooknames as $hookname ) {
     1968                add_action( $hookname, array( $this, 'action_site_status_hook' ), 10, 1 );
     1969            }
     1970        }
     1971
     1972        private function get_listen_to_site_status_hooks_result() {
     1973            $hooknames = array(
     1974                'make_spam_blog',
     1975                'make_ham_blog',
     1976                'mature_blog',
     1977                'unmature_blog',
     1978                'archive_blog',
     1979                'unarchive_blog',
     1980                'make_delete_blog',
     1981                'make_undelete_blog',
     1982                'update_blog_public',
     1983            );
     1984
     1985            foreach ( $hooknames as $hookname ) {
     1986                remove_action( $hookname, array( $this, 'action_site_status_hook' ), 10 );
     1987            }
     1988
     1989            return $this->site_status_hooks;
     1990        }
     1991
     1992        public function action_site_status_hook( $site_id ) {
     1993            $this->site_status_hooks[ current_action() ] = $site_id;
     1994        }
    12621995    }
    12631996
Note: See TracChangeset for help on using the changeset viewer.