Proyectos de Subversion Moodle

Rev

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

Rev 1 Rev 1441
Línea 22... Línea 22...
22
 * @package    tool_usertours
22
 * @package    tool_usertours
23
 * @copyright  2016 Andrew Nicols <andrew@nicols.co.uk>
23
 * @copyright  2016 Andrew Nicols <andrew@nicols.co.uk>
24
 * @license    https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 * @license    https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 * @covers    \tool_usertours\tour
25
 * @covers    \tool_usertours\tour
26
 */
26
 */
27
class tour_test extends \advanced_testcase {
27
final class tour_test extends \advanced_testcase {
28
    /**
28
    /**
29
     * @var moodle_database
29
     * @var moodle_database
30
     */
30
     */
31
    protected $db;
31
    protected $db;
Línea 32... Línea 32...
32
 
32
 
33
    public static function setUpBeforeClass(): void {
33
    public static function setUpBeforeClass(): void {
34
        global $CFG;
34
        global $CFG;
-
 
35
        require_once($CFG->libdir . '/formslib.php');
35
        require_once($CFG->libdir . '/formslib.php');
36
        parent::setUpBeforeClass();
Línea 36... Línea 37...
36
    }
37
    }
37
 
38
 
38
    /**
39
    /**
39
     * Setup to store the DB reference.
40
     * Setup to store the DB reference.
40
     */
41
     */
-
 
42
    public function setUp(): void {
Línea 41... Línea 43...
41
    public function setUp(): void {
43
        global $DB;
42
        global $DB;
44
        parent::setUp();
Línea 43... Línea 45...
43
 
45
 
Línea 49... Línea 51...
49
     */
51
     */
50
    public function tearDown(): void {
52
    public function tearDown(): void {
51
        global $DB;
53
        global $DB;
Línea 52... Línea 54...
52
 
54
 
-
 
55
        $DB = $this->db;
53
        $DB = $this->db;
56
        parent::tearDown();
Línea 54... Línea 57...
54
    }
57
    }
55
 
58
 
56
    /**
59
    /**
Línea 522... Línea 525...
522
            ->willReturn([$step]);
525
            ->willReturn([$step]);
Línea 523... Línea 526...
523
 
526
 
524
        // Mock the database.
527
        // Mock the database.
Línea 525... Línea 528...
525
        $DB = $this->mock_database();
528
        $DB = $this->mock_database();
-
 
529
 
526
 
530
        $deleteinvocations = $this->exactly(3);
-
 
531
        $DB->expects($deleteinvocations)
-
 
532
            ->method('delete_records')
527
        $DB->expects($this->exactly(3))
533
            ->willReturnCallback(function ($table, $conditions) use ($deleteinvocations, $id) {
528
            ->method('delete_records')
534
                switch (self::getInvocationCount($deleteinvocations)) {
-
 
535
                    case 1:
-
 
536
                        $this->assertEquals('tool_usertours_tours', $table);
-
 
537
                        $this->assertEquals(['id' => $id], $conditions);
-
 
538
                        return null;
-
 
539
                        break;
529
            ->withConsecutive(
540
                    case 2:
-
 
541
                        $this->assertEquals('user_preferences', $table);
-
 
542
                        $this->assertEquals(['name' => tour::TOUR_LAST_COMPLETED_BY_USER . $id], $conditions);
-
 
543
                        return null;
-
 
544
                        break;
530
                [$this->equalTo('tool_usertours_tours'), $this->equalTo(['id' => $id])],
545
                    case 3:
-
 
546
                        $this->assertEquals('user_preferences', $table);
-
 
547
                        $this->assertEquals(['name' => tour::TOUR_REQUESTED_BY_USER . $id], $conditions);
-
 
548
                        return null;
-
 
549
                        break;
531
                [$this->equalTo('user_preferences'), $this->equalTo(['name' => tour::TOUR_LAST_COMPLETED_BY_USER . $id])],
550
                    default:
532
                [$this->equalTo('user_preferences'), $this->equalTo(['name' => tour::TOUR_REQUESTED_BY_USER . $id])]
551
                        $this->fail('Unexpected call to delete_records');
Línea 533... Línea 552...
533
            )
552
                }
534
            ->willReturn(null);
553
            });
535
 
554
 
536
        $DB->expects($this->once())
555
        $DB->expects($this->once())
Línea 549... Línea 568...
549
 
568
 
550
        $mockdata = [];
569
        $mockdata = [];
551
        for ($i = 4; $i >= 0; $i--) {
570
        for ($i = 4; $i >= 0; $i--) {
552
            $id = rand($i * 10, ($i * 10) + 9);
571
            $id = rand($i * 10, ($i * 10) + 9);
553
            $mockdata[] = (object) ['id' => $id];
572
            $mockdata[] = (object) ['id' => $id];
554
            $expectations[] = [$this->equalTo('tool_usertours_steps'), $this->equalTo('sortorder'), 4 - $i, ['id' => $id]];
573
            $expectations[] = [4 - $i, ['id' => $id]];
Línea 555... Línea 574...
555
        }
574
        }
556
 
575
 
557
        // Mock the database.
576
        // Mock the database.
558
        $DB = $this->mock_database();
577
        $DB = $this->mock_database();
559
        $DB->expects($this->once())
578
        $DB->expects($this->once())
Línea 560... Línea 579...
560
            ->method('get_records')
579
            ->method('get_records')
-
 
580
            ->willReturn($mockdata);
561
            ->willReturn($mockdata);
581
 
-
 
582
        $setfieldinvocations = $this->exactly(5);
-
 
583
        $DB->expects($setfieldinvocations)
-
 
584
            ->method('set_field')
-
 
585
            ->willReturnCallback(function ($table, $field, $value, $conditions) use (
-
 
586
                $setfieldinvocations,
-
 
587
                $expectations,
-
 
588
            ): void {
-
 
589
                $expectation = $expectations[self::getInvocationCount($setfieldinvocations) - 1];
562
 
590
                $this->assertEquals('tool_usertours_steps', $table);
-
 
591
                $this->assertEquals('sortorder', $field);
Línea 563... Línea 592...
563
        $setfield = $DB->expects($this->exactly(5))
592
                $this->assertEquals($expectation[0], $value);
564
            ->method('set_field');
593
                $this->assertEquals($expectation[1], $conditions);
Línea 565... Línea 594...
565
        call_user_func_array([$setfield, 'withConsecutive'], $expectations);
594
            });
Línea 759... Línea 788...
759
                'persist',
788
                'persist',
760
            ])
789
            ])
761
            ->getMock();
790
            ->getMock();
Línea 762... Línea 791...
762
 
791
 
763
        if ($getconfig) {
792
        if ($getconfig) {
-
 
793
            $getinvocations = $this->exactly(count($getconfig));
764
            $tour->expects($this->exactly(count($getconfig)))
794
            $tour->expects($getinvocations)
765
                ->method('get_config')
795
                ->method('get_config')
-
 
796
                ->willReturnCallback(function () use ($getinvocations, $getconfig) {
-
 
797
                    return $getconfig[self::getInvocationCount($getinvocations) - 1];
766
                ->will(call_user_func_array([$this, 'onConsecutiveCalls'], $getconfig));
798
                });
Línea 767... Línea 799...
767
        }
799
        }
768
 
800
 
769
        if ($setconfig) {
801
        if ($setconfig) {