Proyectos de Subversion Moodle

Rev

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

Rev 11 Rev 1441
Línea 14... Línea 14...
14
// You should have received a copy of the GNU General Public License
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
Línea 16... Línea 16...
16
 
16
 
Línea -... Línea 17...
-
 
17
namespace core;
17
namespace core;
18
 
Línea 18... Línea 19...
18
 
19
use PHPUnit\Framework\Attributes\WithoutErrorHandler;
19
use phpunit_util;
20
use phpunit_util;
20
 
21
 
21
/**
22
/**
22
 * Test basic_testcase extra features and PHPUnit Moodle integration.
23
 * Test basic_testcase extra features and PHPUnit Moodle integration.
23
 *
24
 *
24
 * @package    core
25
 * @package    core
25
 * @category   test
26
 * @category   test
26
 * @copyright  2012 Petr Skoda {@link http://skodak.org}
27
 * @copyright  2012 Petr Skoda {@link http://skodak.org}
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
Línea 28... Línea 29...
28
 */
29
 */
29
class basic_test extends \basic_testcase {
30
final class basic_test extends \basic_testcase {
30
    protected $testassertexecuted = false;
31
    protected $testassertexecuted = false;
Línea 233... Línea 234...
233
     */
234
     */
234
    public function test_db_modification(): void {
235
    public function test_db_modification(): void {
235
        global $DB;
236
        global $DB;
236
        $DB->set_field('user', 'confirmed', 1, ['id' => -1]);
237
        $DB->set_field('user', 'confirmed', 1, ['id' => -1]);
Línea 237... Línea -...
237
 
-
 
238
        // Let's convert the user warnings into an assert-able exception.
-
 
239
        set_error_handler(
-
 
240
            static function ($errno, $errstr) {
-
 
241
                restore_error_handler();
-
 
242
                throw new \Exception($errstr, $errno);
-
 
243
            },
-
 
244
            E_USER_WARNING // Or any other specific E_ that we want to assert.
-
 
245
        );
-
 
246
 
238
 
247
        $this->expectException(\Exception::class);
239
        $this->expectException(\core_phpunit\exception\test_exception::class);
248
        $this->expectExceptionMessage('Warning: unexpected database modification');
240
        $this->expectExceptionMessage('Warning: unexpected database modification');
249
        phpunit_util::reset_all_data(true);
241
        phpunit_util::reset_all_data(true);
Línea 250... Línea 242...
250
    }
242
    }
Línea 259... Línea 251...
259
        global $CFG;
251
        global $CFG;
260
        $CFG->xx = 'yy';
252
        $CFG->xx = 'yy';
261
        unset($CFG->admin);
253
        unset($CFG->admin);
262
        $CFG->rolesactive = 0;
254
        $CFG->rolesactive = 0;
Línea 263... Línea -...
263
 
-
 
264
        // Let's convert the user warnings into an assert-able exception.
-
 
265
        set_error_handler(
-
 
266
            static function ($errno, $errstr) {
-
 
267
                restore_error_handler();
-
 
268
                throw new \Exception($errstr, $errno);
-
 
269
            },
-
 
270
            E_USER_WARNING // Or any other specific E_ that we want to assert.
-
 
271
        );
-
 
272
 
255
 
273
        $this->expectException(\Exception::class);
256
        $this->expectException(\Exception::class);
274
        $this->expectExceptionMessageMatches('/rolesactive.*xx value.*removal.*admin/ms'); // 3 messages matched.
257
        $this->expectExceptionMessageMatches('/rolesactive.*xx value.*removal.*admin/ms'); // 3 messages matched.
275
        phpunit_util::reset_all_data(true);
258
        phpunit_util::reset_all_data(true);
Línea 283... Línea 266...
283
     */
266
     */
284
    public function test_user_modification(): void {
267
    public function test_user_modification(): void {
285
        global $USER;
268
        global $USER;
286
        $USER->id = 10;
269
        $USER->id = 10;
Línea 287... Línea -...
287
 
-
 
288
        // Let's convert the user warnings into an assert-able exception.
-
 
289
        set_error_handler(
-
 
290
            static function ($errno, $errstr) {
-
 
291
                restore_error_handler();
-
 
292
                throw new \Exception($errstr, $errno);
-
 
293
            },
-
 
294
            E_USER_WARNING // Or any other specific E_ that we want to assert.
-
 
295
        );
-
 
296
 
270
 
297
        $this->expectException(\Exception::class);
271
        $this->expectException(\Exception::class);
298
        $this->expectExceptionMessage('Warning: unexpected change of $USER');
272
        $this->expectExceptionMessage('Warning: unexpected change of $USER');
299
        phpunit_util::reset_all_data(true);
273
        phpunit_util::reset_all_data(true);
Línea 307... Línea 281...
307
     */
281
     */
308
    public function test_course_modification(): void {
282
    public function test_course_modification(): void {
309
        global $COURSE;
283
        global $COURSE;
310
        $COURSE->id = 10;
284
        $COURSE->id = 10;
Línea 311... Línea -...
311
 
-
 
312
        // Let's convert the user warnings into an assert-able exception.
-
 
313
        set_error_handler(
-
 
314
            static function ($errno, $errstr) {
-
 
315
                restore_error_handler();
-
 
316
                throw new \Exception($errstr, $errno);
-
 
317
            },
-
 
318
            E_USER_WARNING // Or any other specific E_ that we want to assert.
-
 
319
        );
-
 
320
 
285
 
321
        $this->expectException(\Exception::class);
286
        $this->expectException(\Exception::class);
322
        $this->expectExceptionMessage('Warning: unexpected change of $COURSE');
287
        $this->expectExceptionMessage('Warning: unexpected change of $COURSE');
323
        phpunit_util::reset_all_data(true);
288
        phpunit_util::reset_all_data(true);
Línea 336... Línea 301...
336
        unset($CFG->admin);
301
        unset($CFG->admin);
337
        $CFG->rolesactive = 0;
302
        $CFG->rolesactive = 0;
338
        $USER->id = 10;
303
        $USER->id = 10;
339
        $COURSE->id = 10;
304
        $COURSE->id = 10;
Línea 340... Línea -...
340
 
-
 
341
        // Let's convert the user warnings into an assert-able exception.
-
 
342
        set_error_handler(
-
 
343
            static function ($errno, $errstr) {
-
 
344
                restore_error_handler();
-
 
345
                throw new \Exception($errstr, $errno);
-
 
346
            },
-
 
347
            E_USER_WARNING // Or any other specific E_ that we want to assert.
-
 
348
        );
-
 
349
 
305
 
350
        $this->expectException(\Exception::class);
306
        $this->expectException(\Exception::class);
351
        $this->expectExceptionMessageMatches('/resetting.*rolesactive.*new.*removal.*USER.*COURSE/ms'); // 6 messages matched.
307
        $this->expectExceptionMessageMatches('/resetting.*rolesactive.*new.*removal.*USER.*COURSE/ms'); // 6 messages matched.
352
        phpunit_util::reset_all_data(true);
308
        phpunit_util::reset_all_data(true);
Línea 375... Línea 331...
375
 
331
 
376
        // Assert that the transaction is now closed and the changes were rolled back.
332
        // Assert that the transaction is now closed and the changes were rolled back.
377
        $this->assertFalse($DB->is_transaction_started());
333
        $this->assertFalse($DB->is_transaction_started());
378
        $this->assertEquals($originalname, $DB->get_field('course', 'fullname', ['id' => $COURSE->id]));
334
        $this->assertEquals($originalname, $DB->get_field('course', 'fullname', ['id' => $COURSE->id]));
-
 
335
    }
-
 
336
 
-
 
337
    /**
-
 
338
     * Test that the navigation node URL is overridden correctly.
-
 
339
     */
-
 
340
    public function test_set_navigation_url(): void {
-
 
341
        \navigation_node::override_active_url(new \core\url('/foo/bar/baz'));
-
 
342
        $this->assertNotNull(
-
 
343
            (new \ReflectionClass(\navigation_node::class))->getStaticPropertyValue('fullmeurl', null),
-
 
344
        );
-
 
345
    }
-
 
346
 
-
 
347
    /**
-
 
348
     * Test that the after-test teardown correctly resets the navigation node URL.
-
 
349
     *
-
 
350
     * @depends test_set_navigation_url
-
 
351
     */
-
 
352
    public function test_navigation_url_reset(): void {
-
 
353
        $this->assertNull(
-
 
354
            (new \ReflectionClass(\navigation_node::class))->getStaticPropertyValue('fullmeurl', null),
-
 
355
        );
379
    }
356
    }