ViewTest.php 5.49 KB
Newer Older
1
2
3
4
5
6
<?php
/**
 *
 * @package    mahara
 * @subpackage tests
 * @author     Andrew Nicols
7
8
9
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
 * @copyright  For copyright information on Mahara, please see the README file distributed with this software.
 * @copyright  (C) 2009 Penny Leach
10
 *
11
12
13
14
15
16
17
18
19
20
21
22
 */

require_once(get_config('libroot') . 'view.php');

/**
 * Test the view class
 *
 * TODO this just sets basic creating, setting a fiew fields and deleting
 * need to test access and blocks and all sorts of other things.
 */
class ViewTest extends MaharaUnitTest {

23
24
    /** The id of the user created in setUp. */
    private $testuserid;
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
    /** the view created in setUp */
    private $view;
    /** the id of our shared view. kept for persistency after we delete the view */
    private $viewid;
    /** array of values to set in the view object. TODO: add more */
    private $fields = array(
        'title'       => array(
            'Test View',
            'Test View new title'
        ),
        'description' => array(
            'A view to test with!',
            'a new description to test with'
        ),
        // ....
    );

    /**
     * shared setUp method
     * require a test user, and
     * create a view to test with
     */
    public function setUp() {
48
        parent::setUp();
49
        $this->testuserid = $this->create_test_user();
50
        // set the owner of the view to the test user we created
51
        $this->fields['owner'] = array($this->testuserid, $this->testuserid);
52
53
54
        $this->view = View::create(array(
            'title'       => $this->fields['title'][0],
            'description' => $this->fields['description'][0],
55
        ), $this->testuserid);
56
57
58
59
60
61
62
        $this->viewid = $this->view->get('id');
    }

    /**
     * make sure what got created makes sense
     */
    public function testViewCreating() {
63
        $this->assertInternalType('int', (int) $this->viewid);
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
        $this->assertGreaterThan(0, $this->viewid);

        // now get it again and make sure it matches
        try {
            $createdview = new View($this->view->get('id'));
        }
        catch (Exception $e) {
            $this->fail("Couldn't find new view I created");
        }
        foreach ($this->fields as $field => $values) {
            // make sure both the values in the db and in the return object match what we said
            $this->assertEquals($values[0], $createdview->get($field));
            $this->assertEquals($values[0], $this->view->get($field));
        }
    }

    /**
     * test that the view setters work (without committing)
     */
    public function testViewSetters() {
        foreach ($this->fields as $field => $values) {
            $this->view->set($field, $values[1]);
            $this->assertEquals($values[1], $this->view->get($field));
        }
    }

    /**
     * test that the setters work and commit to the db
     * and when we get the view back it matches
     */
    public function testViewCommitting() {
        // now commit to db and test again
        foreach ($this->fields as $field => $values) {
            $this->view->set($field, $values[1]);
        }
        $this->view->commit();

        $createdview = new View($this->view->get('id'));
        foreach ($this->fields as $field => $values) {
            $this->assertEquals($values[1], $createdview->get($field));
        }
    }

    /**
     * test that when we delete a view,
     * it actually gets deleted from the database
     */
    public function testViewDeleting() {
        $todelete = View::create(array(
            'title'       => $this->fields['title'][0],
            'description' => $this->fields['description'][0],
115
        ), $this->testuserid);
116
117
118
119
120
121
122
123
        $todeleteid = $todelete->get('id');
        $todelete->delete();
        $todelete->commit();
        try {
            $deleted = new View($todeleteid);
            $this->fail("View wasn't deleted properly!");
        }
        catch (Exception $e) {}
124
125
126

        $fromdb = get_field('view', 'type', 'id', $todeleteid);
        $this->assertEquals(null, $fromdb);
127
128
    }

129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
    public function testView_get_artefact_instances() {
        // Empty view should return no artefact instances.
        $this->assertCount(0, $this->view->get_artefact_instances());

        for ($i = 0; $i < 3; $i++) {
            $bi = new BlockInstance(0,
                array(
                    'blocktype'  => 'textbox',
                    'title'      => 'test block ' . $i,
                    'view'       => $this->view->get('id'),
                    'view_obj'   => $this->view,
                    'row'        => $i,
                    'column'     => 0,
                    'order'      => 0,
                )
            );
            $bi->commit();
            $values = PluginBlocktypeTextbox::instance_config_save(
                array(
                    'text' => 'This is note ' . $i,
                    'tags' => null,
                    'artefactids' => null,
                ),
                $bi
            );
            $bi->set('configdata', $values);
            unset($values['_redrawblocks']);
            unset($values['title']);
            $bi->commit();
            $this->view->addblockinstance($bi);
            $this->view->commit();
        }

        $this->assertCount(3, $this->view->get_artefact_instances());
    }

165
166
167
168
169
170
171
172
173
174
    /**
     * clean up after ourselves,
     * just delete the test view we made
     * and call the parent method
     */
    public function tearDown() {
        $this->view->delete();
        parent::tearDown();
    }
}