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;
-
 
18
 
17
namespace core;
19
use core_phpunit\exception\test_exception;
18
 
20
 
19
/**
21
/**
20
 * Test advanced_testcase extra features.
22
 * Test advanced_testcase extra features.
21
 *
23
 *
22
 * @package    core
24
 * @package    core
23
 * @category   test
25
 * @category   test
24
 * @copyright  2012 Petr Skoda {@link http://skodak.org}
26
 * @copyright  2012 Petr Skoda {@link http://skodak.org}
25
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26
 * @coversDefaultClass \advanced_testcase
28
 * @coversDefaultClass \advanced_testcase
27
 */
29
 */
28
class advanced_test extends \advanced_testcase {
30
final class advanced_test extends \advanced_testcase {
29
    public static function setUpBeforeClass(): void {
31
    public static function setUpBeforeClass(): void {
-
 
32
        global $CFG;
30
        global $CFG;
33
        require_once(__DIR__ . '/fixtures/adhoc_test_task.php');
Línea 31... Línea 34...
31
        require_once(__DIR__ . '/fixtures/adhoc_test_task.php');
34
        parent::setUpBeforeClass();
32
    }
35
    }
33
 
36
 
Línea 213... Línea 216...
213
        self::resetAllData(true);
216
        self::resetAllData(true);
Línea 214... Línea 217...
214
 
217
 
215
        // Database change.
218
        // Database change.
216
        $this->assertEquals(1, $DB->get_field('user', 'confirmed', array('id'=>2)));
219
        $this->assertEquals(1, $DB->get_field('user', 'confirmed', array('id'=>2)));
-
 
220
        $DB->set_field('user', 'confirmed', 0, array('id'=>2));
217
        $DB->set_field('user', 'confirmed', 0, array('id'=>2));
221
 
218
        try {
222
        try {
219
            self::resetAllData(true);
223
            self::resetAllData(true);
220
        } catch (\Exception $e) {
224
        } catch (test_exception $e) {
221
            $this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
225
            $this->assertStringContainsString('unexpected database modification', $e->getMessage());
222
        }
-
 
Línea -... Línea 226...
-
 
226
        }
223
        $this->assertEquals(1, $DB->get_field('user', 'confirmed', array('id'=>2)));
227
 
224
 
228
        $this->assertEquals(1, $DB->get_field('user', 'confirmed', array('id'=>2)));
225
        // Config change.
229
        // Config change.
226
        $CFG->xx = 'yy';
230
        $CFG->xx = 'yy';
-
 
231
        unset($CFG->admin);
227
        unset($CFG->admin);
232
        $CFG->rolesactive = 0;
228
        $CFG->rolesactive = 0;
233
 
229
        try {
234
        try {
230
            self::resetAllData(true);
-
 
231
        } catch (\Exception $e) {
235
            self::resetAllData(true);
232
            $this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
236
        } catch (test_exception $e) {
233
            $this->assertStringContainsString('xx', $e->getMessage());
237
            $this->assertStringContainsString('xx', $e->getMessage());
-
 
238
            $this->assertStringContainsString('admin', $e->getMessage());
-
 
239
            $this->assertStringContainsString('rolesactive', $e->getMessage());
-
 
240
            $this->assertFalse(isset($CFG->xx));
234
            $this->assertStringContainsString('admin', $e->getMessage());
241
            $this->assertTrue(isset($CFG->admin));
235
            $this->assertStringContainsString('rolesactive', $e->getMessage());
-
 
236
        }
-
 
237
        $this->assertFalse(isset($CFG->xx));
-
 
Línea 238... Línea 242...
238
        $this->assertTrue(isset($CFG->admin));
242
            $this->assertEquals(1, $CFG->rolesactive);
239
        $this->assertEquals(1, $CFG->rolesactive);
243
        }
240
 
244
 
Línea 266... Línea 270...
266
 
270
 
267
        // COURSE change.
271
        // COURSE change.
268
        $SITE->id = 10;
272
        $SITE->id = 10;
269
        $COURSE = new \stdClass();
273
        $COURSE = new \stdClass();
-
 
274
        $COURSE->id = 7;
270
        $COURSE->id = 7;
275
 
271
        try {
276
        try {
272
            self::resetAllData(true);
277
            self::resetAllData(true);
273
        } catch (\Exception $e) {
278
        } catch (test_exception $e) {
274
            $this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
-
 
275
            $this->assertEquals(1, $SITE->id);
-
 
276
            $this->assertSame($SITE, $COURSE);
-
 
277
            $this->assertSame($SITE, $COURSE);
279
            $this->assertStringContainsString('unexpected change of $COURSE', $e->getMessage());
Línea -... Línea 280...
-
 
280
        }
-
 
281
 
-
 
282
        $this->assertEquals(1, $SITE->id);
-
 
283
        $this->assertSame($SITE, $COURSE);
278
        }
284
        $this->assertSame($SITE, $COURSE);
279
 
285
 
-
 
286
        // USER change.
280
        // USER change.
287
        $this->setUser(2);
281
        $this->setUser(2);
288
 
282
        try {
289
        try {
283
            self::resetAllData(true);
290
            self::resetAllData(true);
284
        } catch (\Exception $e) {
-
 
285
            $this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
291
        } catch (test_exception $e) {
-
 
292
            $this->assertStringContainsString('unexpected change of $USER', $e->getMessage());
-
 
293
        }
-
 
294
 
286
            $this->assertEquals(0, $USER->id);
295
        $this->assertEquals(0, $USER->id);
Línea 287... Línea 296...
287
        }
296
 
288
    }
297
    }
289
 
298
 
290
    public function test_getDataGenerator(): void {
299
    public function test_getDataGenerator(): void {
Línea -... Línea 300...
-
 
300
        $generator = $this->getDataGenerator();
-
 
301
        $this->assertInstanceOf('testing_data_generator', $generator);
-
 
302
    }
-
 
303
 
-
 
304
    /**
-
 
305
     * Some basic tests for the getExternalTestFileUrl() method.
-
 
306
     */
-
 
307
    public function test_external_file_url(): void {
-
 
308
        $url = self::getExternalTestFileUrl('testfile.txt');
-
 
309
        // There should only be a // after the protocol.
-
 
310
        $this->assertCount(2, explode('//', $url));
-
 
311
 
-
 
312
        if (defined('TEST_EXTERNAL_FILES_HTTP_URL')) {
-
 
313
            $this->assertStringContainsString(TEST_EXTERNAL_FILES_HTTP_URL, $url);
-
 
314
        } else {
-
 
315
            $this->assertStringContainsString('http://download.moodle.org/unittest', $url);
-
 
316
        }
-
 
317
 
-
 
318
        $url = self::getExternalTestFileUrl('testfile.txt', true);
-
 
319
        $this->assertCount(2, explode('//', $url));
-
 
320
 
-
 
321
        if (defined('TEST_EXTERNAL_FILES_HTTPS_URL')) {
-
 
322
            $this->assertStringContainsString(TEST_EXTERNAL_FILES_HTTPS_URL, $url);
-
 
323
        } else {
291
        $generator = $this->getDataGenerator();
324
            $this->assertStringContainsString('https://download.moodle.org/unittest', $url);
292
        $this->assertInstanceOf('testing_data_generator', $generator);
325
        }
Línea 293... Línea 326...
293
    }
326
    }
294
 
327
 
Línea 592... Línea 625...
592
 
625
 
593
        $this->setTimezone('99', 'Europe/Prague');
626
        $this->setTimezone('99', 'Europe/Prague');
594
        $this->assertSame('99', $CFG->timezone);
627
        $this->assertSame('99', $CFG->timezone);
Línea -... Línea 628...
-
 
628
        $this->assertSame('Europe/Prague', date_default_timezone_get());
-
 
629
 
-
 
630
        // Catch warning for invalid 'xxx' timezone.
-
 
631
        set_error_handler(function ($errno, $errstr): void {
595
        $this->assertSame('Europe/Prague', date_default_timezone_get());
632
            $this->assertStringContainsString('Unknown or bad timezone', $errstr);
-
 
633
        }, E_WARNING);
596
 
634
        $this->setTimezone('xxx', 'Europe/Prague');
597
        $this->setTimezone('xxx', 'Europe/Prague');
635
        restore_error_handler();
Línea 598... Línea 636...
598
        $this->assertSame('xxx', $CFG->timezone);
636
        $this->assertSame('xxx', $CFG->timezone);
599
        $this->assertSame('Europe/Prague', date_default_timezone_get());
637
        $this->assertSame('Europe/Prague', date_default_timezone_get());
600
 
638
 
Línea 601... Línea 639...
601
        $this->setTimezone();
639
        $this->setTimezone();
602
        $this->assertSame('Australia/Perth', $CFG->timezone);
640
        $this->assertSame('Australia/Perth', $CFG->timezone);
603
        $this->assertSame('Australia/Perth', date_default_timezone_get());
641
        $this->assertSame('Australia/Perth', date_default_timezone_get());
-
 
642
 
604
 
643
        // Catch warnings for other invalid cases.
605
        try {
644
        $invalidtimezones = ['', 'xxxx', null];
606
            $this->setTimezone('Pacific/Auckland', '');
-
 
607
        } catch (\Exception $e) {
645
        foreach ($invalidtimezones as $invalidtz) {
608
            $this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
646
            set_error_handler(function ($errno, $errstr): void {
609
        }
647
                $this->assertStringContainsString('Unknown or bad timezone', $errstr);
610
 
648
            }, E_WARNING);
611
        try {
-
 
612
            $this->setTimezone('Pacific/Auckland', 'xxxx');
-
 
613
        } catch (\Exception $e) {
649
            try {
614
            $this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
-
 
615
        }
650
                $this->setTimezone('Pacific/Auckland', $invalidtz);
616
 
-
 
617
        try {
651
            } catch (\Throwable $e) {
Línea 618... Línea 652...
618
            $this->setTimezone('Pacific/Auckland', null);
652
                $this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
Línea 619... Línea 653...
619
        } catch (\Exception $e) {
653
            }
Línea 639... Línea 673...
639
            setlocale(LC_TIME, 'en_US.UTF-8');
673
            setlocale(LC_TIME, 'en_US.UTF-8');
640
        }
674
        }
Línea 641... Línea 675...
641
 
675
 
642
        try {
676
        try {
643
            self::resetAllData(true);
677
            self::resetAllData(true);
644
        } catch (\Exception $e) {
678
        } catch (test_exception $e) {
645
            $this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
679
            $this->assertStringContainsString('unexpected change of locale', $e->getMessage());
Línea 646... Línea 680...
646
        }
680
        }
647
 
681
 
648
        if ($CFG->ostype === 'WINDOWS') {
682
        if ($CFG->ostype === 'WINDOWS') {
Línea 754... Línea 788...
754
 
788
 
755
        // Test the functionality.
789
        // Test the functionality.
756
        $this->assertEquals(0, $mockedclock->now()->getTimestamp());
790
        $this->assertEquals(0, $mockedclock->now()->getTimestamp());
757
        $this->assertEquals(1, $newclock->now()->getTimestamp());
791
        $this->assertEquals(1, $newclock->now()->getTimestamp());
-
 
792
        $this->assertEquals(2, $mockedclock->now()->getTimestamp());
Línea 758... Línea 793...
758
        $this->assertEquals(2, $mockedclock->now()->getTimestamp());
793
        $this->assertEquals('Australia/Perth', $newclock->now()->getTimezone()->getName());
759
 
794
 
760
        // Specify a specific start time.
795
        // Specify a specific start time.
761
        $newclock = $this->mock_clock_with_incrementing(12345);
796
        $newclock = $this->mock_clock_with_incrementing(12345);
Línea 765... Línea 800...
765
        $this->assertEquals(12345, $mockedclock->now()->getTimestamp());
800
        $this->assertEquals(12345, $mockedclock->now()->getTimestamp());
766
        $this->assertEquals(12346, $newclock->now()->getTimestamp());
801
        $this->assertEquals(12346, $newclock->now()->getTimestamp());
767
        $this->assertEquals(12347, $mockedclock->now()->getTimestamp());
802
        $this->assertEquals(12347, $mockedclock->now()->getTimestamp());
Línea 768... Línea 803...
768
 
803
 
-
 
804
        $this->assertEquals($newclock->time, $mockedclock->now()->getTimestamp());
769
        $this->assertEquals($newclock->time, $mockedclock->now()->getTimestamp());
805
        $this->assertEquals('Australia/Perth', $newclock->now()->getTimezone()->getName());
Línea 770... Línea 806...
770
    }
806
    }
771
 
807
 
772
    /**
808
    /**
Línea 787... Línea 823...
787
 
823
 
788
        // Test the functionality.
824
        // Test the functionality.
789
        $initialtime = $mockedclock->now()->getTimestamp();
825
        $initialtime = $mockedclock->now()->getTimestamp();
790
        $this->assertEquals($initialtime, $newclock->now()->getTimestamp());
826
        $this->assertEquals($initialtime, $newclock->now()->getTimestamp());
-
 
827
        $this->assertEquals($initialtime, $mockedclock->now()->getTimestamp());
Línea 791... Línea 828...
791
        $this->assertEquals($initialtime, $mockedclock->now()->getTimestamp());
828
        $this->assertEquals('Australia/Perth', $newclock->now()->getTimezone()->getName());
792
 
829
 
793
        // Specify a specific start time.
830
        // Specify a specific start time.
794
        $newclock = $this->mock_clock_with_frozen(12345);
831
        $newclock = $this->mock_clock_with_frozen(12345);
Línea 795... Línea 832...
795
        $mockedclock = \core\di::get(\core\clock::class);
832
        $mockedclock = \core\di::get(\core\clock::class);
796
        $this->assertSame($newclock, $mockedclock);
833
        $this->assertSame($newclock, $mockedclock);
797
 
834
 
798
        $initialtime = $mockedclock->now();
835
        $initialtime = $mockedclock->now();
-
 
836
        $this->assertEquals($initialtime, $mockedclock->now());
799
        $this->assertEquals($initialtime, $mockedclock->now());
837
        $this->assertEquals($initialtime, $newclock->now());
800
        $this->assertEquals($initialtime, $newclock->now());
838
        $this->assertEquals($initialtime, $mockedclock->now());