Merge branch 'MDL-83468-main' of https://github.com/andrewnicols/moodle
This commit is contained in:
@@ -16,6 +16,8 @@
|
||||
|
||||
namespace core;
|
||||
|
||||
use core_phpunit\exception\test_exception;
|
||||
|
||||
/**
|
||||
* Test advanced_testcase extra features.
|
||||
*
|
||||
@@ -216,28 +218,29 @@ final class advanced_test extends \advanced_testcase {
|
||||
// Database change.
|
||||
$this->assertEquals(1, $DB->get_field('user', 'confirmed', array('id'=>2)));
|
||||
$DB->set_field('user', 'confirmed', 0, array('id'=>2));
|
||||
|
||||
try {
|
||||
self::resetAllData(true);
|
||||
} catch (\Exception $e) {
|
||||
$this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
|
||||
} catch (test_exception $e) {
|
||||
$this->assertStringContainsString('unexpected database modification', $e->getMessage());
|
||||
}
|
||||
$this->assertEquals(1, $DB->get_field('user', 'confirmed', array('id'=>2)));
|
||||
|
||||
$this->assertEquals(1, $DB->get_field('user', 'confirmed', array('id'=>2)));
|
||||
// Config change.
|
||||
$CFG->xx = 'yy';
|
||||
unset($CFG->admin);
|
||||
$CFG->rolesactive = 0;
|
||||
|
||||
try {
|
||||
self::resetAllData(true);
|
||||
} catch (\Exception $e) {
|
||||
$this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
|
||||
} catch (test_exception $e) {
|
||||
$this->assertStringContainsString('xx', $e->getMessage());
|
||||
$this->assertStringContainsString('admin', $e->getMessage());
|
||||
$this->assertStringContainsString('rolesactive', $e->getMessage());
|
||||
$this->assertFalse(isset($CFG->xx));
|
||||
$this->assertTrue(isset($CFG->admin));
|
||||
$this->assertEquals(1, $CFG->rolesactive);
|
||||
}
|
||||
$this->assertFalse(isset($CFG->xx));
|
||||
$this->assertTrue(isset($CFG->admin));
|
||||
$this->assertEquals(1, $CFG->rolesactive);
|
||||
|
||||
// _GET change.
|
||||
$_GET['__somethingthatwillnotnormallybepresent__'] = 'yy';
|
||||
@@ -269,23 +272,28 @@ final class advanced_test extends \advanced_testcase {
|
||||
$SITE->id = 10;
|
||||
$COURSE = new \stdClass();
|
||||
$COURSE->id = 7;
|
||||
|
||||
try {
|
||||
self::resetAllData(true);
|
||||
} catch (\Exception $e) {
|
||||
$this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
|
||||
$this->assertEquals(1, $SITE->id);
|
||||
$this->assertSame($SITE, $COURSE);
|
||||
$this->assertSame($SITE, $COURSE);
|
||||
} catch (test_exception $e) {
|
||||
$this->assertStringContainsString('unexpected change of $COURSE', $e->getMessage());
|
||||
}
|
||||
|
||||
$this->assertEquals(1, $SITE->id);
|
||||
$this->assertSame($SITE, $COURSE);
|
||||
$this->assertSame($SITE, $COURSE);
|
||||
|
||||
// USER change.
|
||||
$this->setUser(2);
|
||||
|
||||
try {
|
||||
self::resetAllData(true);
|
||||
} catch (\Exception $e) {
|
||||
$this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
|
||||
$this->assertEquals(0, $USER->id);
|
||||
} catch (test_exception $e) {
|
||||
$this->assertStringContainsString('unexpected change of $USER', $e->getMessage());
|
||||
}
|
||||
|
||||
$this->assertEquals(0, $USER->id);
|
||||
|
||||
}
|
||||
|
||||
public function test_getDataGenerator(): void {
|
||||
@@ -666,8 +674,8 @@ final class advanced_test extends \advanced_testcase {
|
||||
|
||||
try {
|
||||
self::resetAllData(true);
|
||||
} catch (\Exception $e) {
|
||||
$this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
|
||||
} catch (test_exception $e) {
|
||||
$this->assertStringContainsString('unexpected change of locale', $e->getMessage());
|
||||
}
|
||||
|
||||
if ($CFG->ostype === 'WINDOWS') {
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
namespace core;
|
||||
|
||||
use PHPUnit\Framework\Attributes\WithoutErrorHandler;
|
||||
use phpunit_util;
|
||||
|
||||
/**
|
||||
@@ -235,16 +236,7 @@ STRING;
|
||||
global $DB;
|
||||
$DB->set_field('user', 'confirmed', 1, ['id' => -1]);
|
||||
|
||||
// Let's convert the user warnings into an assert-able exception.
|
||||
set_error_handler(
|
||||
static function ($errno, $errstr) {
|
||||
restore_error_handler();
|
||||
throw new \Exception($errstr, $errno);
|
||||
},
|
||||
E_USER_WARNING // Or any other specific E_ that we want to assert.
|
||||
);
|
||||
|
||||
$this->expectException(\Exception::class);
|
||||
$this->expectException(\core_phpunit\exception\test_exception::class);
|
||||
$this->expectExceptionMessage('Warning: unexpected database modification');
|
||||
phpunit_util::reset_all_data(true);
|
||||
}
|
||||
@@ -261,15 +253,6 @@ STRING;
|
||||
unset($CFG->admin);
|
||||
$CFG->rolesactive = 0;
|
||||
|
||||
// Let's convert the user warnings into an assert-able exception.
|
||||
set_error_handler(
|
||||
static function ($errno, $errstr) {
|
||||
restore_error_handler();
|
||||
throw new \Exception($errstr, $errno);
|
||||
},
|
||||
E_USER_WARNING // Or any other specific E_ that we want to assert.
|
||||
);
|
||||
|
||||
$this->expectException(\Exception::class);
|
||||
$this->expectExceptionMessageMatches('/rolesactive.*xx value.*removal.*admin/ms'); // 3 messages matched.
|
||||
phpunit_util::reset_all_data(true);
|
||||
@@ -285,15 +268,6 @@ STRING;
|
||||
global $USER;
|
||||
$USER->id = 10;
|
||||
|
||||
// Let's convert the user warnings into an assert-able exception.
|
||||
set_error_handler(
|
||||
static function ($errno, $errstr) {
|
||||
restore_error_handler();
|
||||
throw new \Exception($errstr, $errno);
|
||||
},
|
||||
E_USER_WARNING // Or any other specific E_ that we want to assert.
|
||||
);
|
||||
|
||||
$this->expectException(\Exception::class);
|
||||
$this->expectExceptionMessage('Warning: unexpected change of $USER');
|
||||
phpunit_util::reset_all_data(true);
|
||||
@@ -309,15 +283,6 @@ STRING;
|
||||
global $COURSE;
|
||||
$COURSE->id = 10;
|
||||
|
||||
// Let's convert the user warnings into an assert-able exception.
|
||||
set_error_handler(
|
||||
static function ($errno, $errstr) {
|
||||
restore_error_handler();
|
||||
throw new \Exception($errstr, $errno);
|
||||
},
|
||||
E_USER_WARNING // Or any other specific E_ that we want to assert.
|
||||
);
|
||||
|
||||
$this->expectException(\Exception::class);
|
||||
$this->expectExceptionMessage('Warning: unexpected change of $COURSE');
|
||||
phpunit_util::reset_all_data(true);
|
||||
@@ -338,15 +303,6 @@ STRING;
|
||||
$USER->id = 10;
|
||||
$COURSE->id = 10;
|
||||
|
||||
// Let's convert the user warnings into an assert-able exception.
|
||||
set_error_handler(
|
||||
static function ($errno, $errstr) {
|
||||
restore_error_handler();
|
||||
throw new \Exception($errstr, $errno);
|
||||
},
|
||||
E_USER_WARNING // Or any other specific E_ that we want to assert.
|
||||
);
|
||||
|
||||
$this->expectException(\Exception::class);
|
||||
$this->expectExceptionMessageMatches('/resetting.*rolesactive.*new.*removal.*USER.*COURSE/ms'); // 6 messages matched.
|
||||
phpunit_util::reset_all_data(true);
|
||||
|
||||
@@ -199,7 +199,7 @@ final class phpunit_dataset_test extends advanced_testcase {
|
||||
'rows' => [],
|
||||
],
|
||||
'csv loads ok' => [
|
||||
'fullpath' => file_get_contents(__DIR__ . '/fixtures/sample_dataset.csv'),
|
||||
'content' => file_get_contents(__DIR__ . '/fixtures/sample_dataset.csv'),
|
||||
'type' => 'csv',
|
||||
'tablename' => 'user',
|
||||
'exception' => null,
|
||||
@@ -215,7 +215,7 @@ final class phpunit_dataset_test extends advanced_testcase {
|
||||
],
|
||||
],
|
||||
'xml loads ok' => [
|
||||
'fullpath' => file_get_contents(__DIR__ . '/fixtures/sample_dataset.xml'),
|
||||
'content' => file_get_contents(__DIR__ . '/fixtures/sample_dataset.xml'),
|
||||
'type' => 'xml',
|
||||
'tablename' => 'user',
|
||||
'exception' => null,
|
||||
|
||||
Reference in New Issue
Block a user