Proyectos de Subversion Moodle

Rev

Rev 11 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 11 Rev 1441
Línea 31... Línea 31...
31
require_once($CFG->dirroot . '/calendar/tests/helpers.php');
31
require_once($CFG->dirroot . '/calendar/tests/helpers.php');
Línea 32... Línea 32...
32
 
32
 
33
/**
33
/**
34
 * Tests various classes and functions in upgradelib.php library.
34
 * Tests various classes and functions in upgradelib.php library.
35
 */
35
 */
Línea 36... Línea 36...
36
class upgradelib_test extends advanced_testcase {
36
final class upgradelib_test extends advanced_testcase {
37
 
37
 
38
    /**
38
    /**
39
     * Test the {@link upgrade_stale_php_files_present() function
39
     * Test the {@link upgrade_stale_php_files_present() function
Línea 742... Línea 742...
742
    /**
742
    /**
743
     * Data provider of serialized string.
743
     * Data provider of serialized string.
744
     *
744
     *
745
     * @return array
745
     * @return array
746
     */
746
     */
747
    public function serialized_strings_dataprovider() {
747
    public static function serialized_strings_dataprovider(): array {
748
        return [
748
        return [
749
            'A configuration that uses the old object' => [
749
            'A configuration that uses the old object' => [
750
                'O:6:"object":3:{s:4:"text";s:32:"Nothing that anyone cares about.";s:5:"title";s:16:"Really old block";s:6:"format";s:1:"1";}',
750
                'O:6:"object":3:{s:4:"text";s:32:"Nothing that anyone cares about.";s:5:"title";s:16:"Really old block";s:6:"format";s:1:"1";}',
751
                true,
751
                true,
752
                'O:8:"stdClass":3:{s:4:"text";s:32:"Nothing that anyone cares about.";s:5:"title";s:16:"Really old block";s:6:"format";s:1:"1";}'
752
                'O:8:"stdClass":3:{s:4:"text";s:32:"Nothing that anyone cares about.";s:5:"title";s:16:"Really old block";s:6:"format";s:1:"1";}'
Línea 1389... Línea 1389...
1389
            $this->assertFalse($result->getStatus());
1389
            $this->assertFalse($result->getStatus());
1390
        }
1390
        }
1391
    }
1391
    }
Línea 1392... Línea 1392...
1392
 
1392
 
1393
    /**
-
 
1394
     * Test the check_oracle_usage check when the Moodle instance is not using Oracle as a database architecture.
-
 
1395
     *
-
 
1396
     * @covers ::check_oracle_usage
-
 
1397
     */
-
 
1398
    public function test_check_oracle_usage_is_not_used(): void {
-
 
1399
        global $CFG;
-
 
1400
 
-
 
1401
        $this->resetAfterTest();
-
 
1402
        $CFG->dbtype = 'pgsql';
-
 
1403
 
-
 
1404
        $result = new environment_results('custom_checks');
-
 
1405
        $this->assertNull(check_oracle_usage($result));
-
 
1406
    }
-
 
1407
 
-
 
1408
    /**
-
 
1409
     * Test the check_oracle_usage check when the Moodle instance is using Oracle as a database architecture.
-
 
1410
     *
-
 
1411
     * @covers ::check_oracle_usage
-
 
1412
     */
-
 
1413
    public function test_check_oracle_usage_is_used(): void {
-
 
1414
        global $CFG;
-
 
1415
 
-
 
1416
        $this->resetAfterTest();
-
 
1417
        $CFG->dbtype = 'oci';
-
 
1418
 
-
 
1419
        $result = new environment_results('custom_checks');
-
 
1420
        $this->assertInstanceOf(environment_results::class, check_oracle_usage($result));
-
 
1421
        $this->assertEquals('oracle_database_usage', $result->getInfo());
-
 
1422
        $this->assertFalse($result->getStatus());
-
 
1423
    }
-
 
1424
 
-
 
1425
    /**
1393
    /**
1426
     * Data provider of usermenu items.
1394
     * Data provider of usermenu items.
1427
     *
1395
     *
1428
     * @return array
1396
     * @return array
1429
     */
1397
     */
1430
    public function usermenu_items_dataprovider(): array {
1398
    public static function usermenu_items_dataprovider(): array {
1431
        return [
1399
        return [
1432
            'Add new item to empty usermenu' => [
1400
            'Add new item to empty usermenu' => [
1433
                '',
1401
                '',
1434
                'reports,core_reportbuilder|/reportbuilder/index.php',
1402
                'reports,core_reportbuilder|/reportbuilder/index.php',
Línea 1638... Línea 1606...
1638
        set_config('upgraderunning', null);
1606
        set_config('upgraderunning', null);
1639
        upgrade_set_timeout(120);
1607
        upgrade_set_timeout(120);
1640
        $upgrade = get_config('core', 'upgraderunning');
1608
        $upgrade = get_config('core', 'upgraderunning');
1641
        $this->assertFalse($upgrade);
1609
        $this->assertFalse($upgrade);
1642
    }
1610
    }
-
 
1611
 
-
 
1612
    /**
-
 
1613
     * Data provider for {@see test_upgrade_change_binary_column_to_int()}.
-
 
1614
     *
-
 
1615
     * @return array[]
-
 
1616
     */
-
 
1617
    public static function upgrade_change_binary_column_to_int_provider(): array {
-
 
1618
        return [
-
 
1619
            'Binary column' => [
-
 
1620
                XMLDB_TYPE_BINARY,
-
 
1621
                null,
-
 
1622
                true,
-
 
1623
                false,
-
 
1624
            ],
-
 
1625
            'Integer column' => [
-
 
1626
                XMLDB_TYPE_INTEGER,
-
 
1627
                '1',
-
 
1628
                false,
-
 
1629
                false,
-
 
1630
            ],
-
 
1631
            'Non-binary and non-integer column' => [
-
 
1632
                XMLDB_TYPE_TEXT,
-
 
1633
                null,
-
 
1634
                false,
-
 
1635
                true,
-
 
1636
            ],
-
 
1637
        ];
-
 
1638
    }
-
 
1639
 
-
 
1640
    /**
-
 
1641
     * Unit test for {@see upgrade_change_binary_column_to_int()}.
-
 
1642
     *
-
 
1643
     * @dataProvider upgrade_change_binary_column_to_int_provider
-
 
1644
     * @covers ::upgrade_change_binary_column_to_int()
-
 
1645
     * @param int $type The field type.
-
 
1646
     * @param string|null $length The field length.
-
 
1647
     * @param bool $expectedresult Whether the conversion succeeded.
-
 
1648
     * @param bool $expecexception Whether to expect an exception.
-
 
1649
     * @return void
-
 
1650
     */
-
 
1651
    public function test_upgrade_change_binary_column_to_int(
-
 
1652
        int $type,
-
 
1653
        ?string $length,
-
 
1654
        bool $expectedresult,
-
 
1655
        bool $expecexception,
-
 
1656
    ): void {
-
 
1657
        global $DB;
-
 
1658
        $this->resetAfterTest();
-
 
1659
 
-
 
1660
        $dbman = $DB->get_manager();
-
 
1661
        $tmptablename = 'test_convert_table';
-
 
1662
        $fieldname = 'success';
-
 
1663
        $table = new xmldb_table($tmptablename);
-
 
1664
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE);
-
 
1665
        $table->add_field($fieldname, $type, $length, null, XMLDB_NOTNULL);
-
 
1666
        $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
-
 
1667
        $dbman->create_table($table);
-
 
1668
 
-
 
1669
        // Insert sample data.
-
 
1670
        $ones = [];
-
 
1671
        $truerecord = (object)[$fieldname => 1];
-
 
1672
        $falserecord = (object)[$fieldname => 0];
-
 
1673
        $ones[] = $DB->insert_record($tmptablename, $truerecord);
-
 
1674
        $DB->insert_record($tmptablename, $falserecord);
-
 
1675
        $ones[] = $DB->insert_record($tmptablename, $truerecord);
-
 
1676
        $DB->insert_record($tmptablename, $falserecord);
-
 
1677
        $ones[] = $DB->insert_record($tmptablename, $truerecord);
-
 
1678
        $ones[] = $DB->insert_record($tmptablename, $truerecord);
-
 
1679
 
-
 
1680
        if ($expecexception) {
-
 
1681
            $this->expectException(coding_exception::class);
-
 
1682
        }
-
 
1683
 
-
 
1684
        $result = upgrade_change_binary_column_to_int($tmptablename, $fieldname);
-
 
1685
        $this->assertEquals($expectedresult, $result);
-
 
1686
 
-
 
1687
        // Verify converted column and data.
-
 
1688
        if ($result) {
-
 
1689
            $columns = $DB->get_columns($tmptablename);
-
 
1690
            // Verify the new field has been created and is no longer a binary field.
-
 
1691
            $this->assertArrayHasKey($fieldname, $columns);
-
 
1692
            $field = $columns[$fieldname];
-
 
1693
            $this->assertFalse($field->binary);
-
 
1694
 
-
 
1695
            // Verify that the renamed old field has now been removed.
-
 
1696
            $this->assertArrayNotHasKey("tmp$fieldname", $columns);
-
 
1697
 
-
 
1698
            // Confirm that the values for the converted column are the same.
-
 
1699
            $records = $DB->get_fieldset($tmptablename, 'id', [$fieldname => 1]);
-
 
1700
            $this->assertEqualsCanonicalizing($ones, $records);
-
 
1701
        }
-
 
1702
 
-
 
1703
        // Cleanup.
-
 
1704
        $dbman->drop_table($table);
-
 
1705
    }
-
 
1706
 
-
 
1707
    /**
-
 
1708
     * Test for upgrade script replacing full urls with relative urls in defaulthomepage setting
-
 
1709
     *
-
 
1710
     * @covers ::upgrade_change_binary_column_to_int()
-
 
1711
     */
-
 
1712
    public function test_upgrade_store_relative_url_sitehomepage(): void {
-
 
1713
        global $CFG;
-
 
1714
        $this->resetAfterTest();
-
 
1715
 
-
 
1716
        // Check updating the value for the defaulthomepage.
-
 
1717
        $CFG->defaulthomepage = $CFG->wwwroot . '/page1';
-
 
1718
        upgrade_store_relative_url_sitehomepage();
-
 
1719
        $this->assertEquals('/page1', $CFG->defaulthomepage);
-
 
1720
 
-
 
1721
        $CFG->defaulthomepage = HOMEPAGE_SITE;
-
 
1722
        upgrade_store_relative_url_sitehomepage();
-
 
1723
        $this->assertEquals(HOMEPAGE_SITE, $CFG->defaulthomepage);
-
 
1724
 
-
 
1725
        // Check updating user preferences.
-
 
1726
        $user1 = $this->getDataGenerator()->create_user();
-
 
1727
        $user2 = $this->getDataGenerator()->create_user();
-
 
1728
        set_user_preference('user_home_page_preference', $CFG->wwwroot . '/page2', $user1);
-
 
1729
        set_user_preference('user_home_page_preference', HOMEPAGE_MY, $user2);
-
 
1730
        upgrade_store_relative_url_sitehomepage();
-
 
1731
        $this->assertEquals('/page2', get_user_preferences('user_home_page_preference', null, $user1->id));
-
 
1732
        $this->assertEquals(HOMEPAGE_MY, get_user_preferences('user_home_page_preference', null, $user2->id));
-
 
1733
    }
-
 
1734
 
-
 
1735
    /**
-
 
1736
     * Test the check_aurora_version check when the Moodle instance is not using Amazon Aurora as a database architecture.
-
 
1737
     *
-
 
1738
     * @covers ::check_aurora_version
-
 
1739
     */
-
 
1740
    public function test_check_aurora_version_is_not_used(): void {
-
 
1741
        global $CFG;
-
 
1742
 
-
 
1743
        $this->resetAfterTest();
-
 
1744
        $CFG->dbtype = 'pgsql';
-
 
1745
 
-
 
1746
        $result = new environment_results('custom_checks');
-
 
1747
        $this->assertNull(check_aurora_version($result));
-
 
1748
    }
-
 
1749
 
-
 
1750
    /**
-
 
1751
     * Test the check_aurora_version check when the Moodle instance is using Amazon Aurora as a database architecture.
-
 
1752
     *
-
 
1753
     * @covers ::check_aurora_version
-
 
1754
     */
-
 
1755
    public function test_check_aurora_version_is_used(): void {
-
 
1756
        global $CFG;
-
 
1757
 
-
 
1758
        $this->resetAfterTest();
-
 
1759
        $CFG->dbtype = 'auroramysql';
-
 
1760
 
-
 
1761
        $result = new environment_results('custom_checks');
-
 
1762
        $this->assertInstanceOf(environment_results::class, check_aurora_version($result));
-
 
1763
        $this->assertEquals('Aurora compatibility', $result->getInfo());
-
 
1764
        $this->assertFalse($result->getStatus());
-
 
1765
    }
1643
}
1766
}