GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

NotificationsManager_MoveTest::tearDown()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
/**
3
 * Copyright (c) Xerox, 2006. All Rights Reserved.
4
 *
5
 * Originally written by Nicolas Terray, 2006.
6
 *
7
 * This file is a part of Codendi.
8
 *
9
 * Codendi is free software; you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation; either version 2 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * Codendi is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with Codendi; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22
 *
23
 *
24
 */
25
26
require_once 'bootstrap.php';
27
28
Mock::generatePartial('Docman_NotificationsManager_Move', 'Docman_NotificationsManager_MoveTestVersion',
29
    array(
30
        '_groupGetObject',
31
        '_getItemFactory',
32
        '_getUserManager',
33
        '_getPermissionsManager',
34
        '_getDocmanPath',
35
        '_getDao',
36
        '_buildMessage',
37
    )
38
);
39
40
Mock::generate('DataAccessResult');
41
42
Mock::generate('BaseLanguage');
43
44
Mock::generate('Group');
45
46
Mock::generate('Feedback');
47
48
Mock::generate('PFUser');
49
50
Mock::generate('UserManager');
51
52
Mock::generate('Docman_ItemFactory');
53
54
Mock::generate('Docman_Item');
55
56
Mock::generate('Docman_PermissionsManager');
57
58
Mock::generate('Docman_Path');
59
60
Mock::generate('NotificationsDao');
61
62
class NotificationsManager_MoveTest extends UnitTestCase {
63
    var $groupId;
64
65
    function setUp() {
66
        $GLOBALS['sys_noreply'] = '[email protected]';
67
        ForgeConfig::store();
68
        ForgeConfig::set('codendi_dir', '/tuleap');
69
    }
70
71
    function tearDown() {
72
        unset($GLOBALS['sys_noreply']);
73
        ForgeConfig::restore();
74
    }
75
76
    function testNotifications() {
77
        // {{{ Listener cannot read moved item
78
        // We expect no notification
79
        $dr = 0;
80
        for($br = 0 ; $br <= 1 ; ++$br) {
81
            for($cr = 0 ; $cr <= 1 ; ++$cr) {
82
                for ($lb = 0 ; $lb <= 1 ; ++$lb) {
83
                    for ($lc = 0 ; $lc <= 1 ; ++$lc) {
84
                        for ($ld = 0 ; $ld <= 1 ; ++$ld) {
85
                            $this->_runTest($dr, $br, $cr, $lb, $lc, $ld, 'none');
86
                        }
87
                    }
88
                }
89
            }
90
        }
91
        //}}}
92
        // {{{ Listener can read moved item
93
        $dr = 1;
94
            // {{{ Listener cannot read old parent
95
            $br = 0;
96
                // {{{ Listener cannot read new parent
97
                // We expect no notification
98
                $cr = 0;
99
                for ($lb = 0 ; $lb <= 1 ; ++$lb) {
100
                    for ($lc = 0 ; $lc <= 1 ; ++$lc) {
101
                        for ($ld = 0 ; $ld <= 1 ; ++$ld) {
102
                            $this->_runTest($dr, $br, $cr, $lb, $lc, $ld, 'none');
103
                        }
104
                    }
105
                }
106
                //}}}
107
                // {{{ Listener can read new parent
108
                // => A readable item is moved from an unreadable parent to a readable one
109
                $cr = 1;
110
                    //{{{ Do not listen item but maybe its parent ?
111
                    $ld = 0;
112
                    // No listeners, no notification
113
                    $this->_runTest($dr, $br, $cr, 0, 0, $ld, 'none');
114
                    // Only old parent is listened (but still unreadable), no notification
115
                    $this->_runTest($dr, $br, $cr, 1, 0, $ld, 'none');
116
                        // {{{ new parent is listened, we receive a notification without b because it is still unreadable
117
                        for ($lb = 0 ; $lb <= 1 ; ++$lb) {
118
                            $this->_runTest($dr, $br, $cr, $lb, 1, $ld, 'to_wo_b');
119
                        }
120
                        //}}}
121
                    //}}}
122
123
                    //{{{ If we listen item, we receive a notification about item ("has been moved to c")
124
                    $ld = 1;
125
                    for ($lb = 0 ; $lb <= 1 ; ++$lb) {
126
                        for ($lc = 0 ; $lc <= 1 ; ++$lc) {
127
                            $this->_runTest($dr, $br, $cr, $lb, $lc, $ld, 'item');
128
                        }
129
                    }
130
                    //}}}
131
                //}}}
132
            //}}}
133
            // {{{ Listener can read old parent
134
            $br = 1;
135
                // {{{ Listener cannot read new parent
136
                // We have to send notifications only when old parent or item is listened
137
                $cr = 0;
138
                for ($lb = 0 ; $lb <= 1 ; ++$lb) {
139
                    for ($lc = 0 ; $lc <= 1 ; ++$lc) {
140
                        for ($ld = 0 ; $ld <= 1 ; ++$ld) {
141
                            $this->_runTest($dr, $br, $cr, $lb, $lc, $ld, $lb || $ld ? 'from_wo_c' : 'none');
142
                        }
143
                    }
144
                }
145
                //}}}
146
                // {{{ Listener can read new parent
147
                $cr = 1;
148
                    // {{{ Moved item is listened, notification on item
149
                    $ld = 1;
150
                    for ($lb = 0 ; $lb <= 1 ; ++$lb) {
151
                        for ($lc = 0 ; $lc <= 1 ; ++$lc) {
152
                            $this->_runTest($dr, $br, $cr, $lb, $lc, $ld, 'item');
153
                        }
154
                    }
155
                    //}}}
156
                    //{{{ Moved item is not listened
157
                    $ld = 0;
158
                        // {{{ new parent is listened, notification 'to'
159
                        $lc = 1;
160
                        for ($lb = 0 ; $lb <= 1 ; ++$lb) {
161
                            $this->_runTest($dr, $br, $cr, $lb, $lc, $ld, 'to');
162
                        }
163
                        //}}}
164
                        // {{{ new parent is not listened
165
                        $lc = 0;
166
                        //Old parent is listened, 'from' notification
167
                        $this->_runTest($dr, $br, $cr, 1, $lc, $ld, 'from');
168
                        //No listener, no notification
169
                        $this->_runTest($dr, $br, $cr, 0, $lc, $ld, 'none');
170
                        //}}}
171
                    //}}}
172
                //}}}
173
            //}}}
174
        //}}}
175
    }
176
    /**
177
    *        A
178
    *        |-- B
179
    *        | +-+-----+
180
    *        | | `-- D |
181
    *        | +-------+ \
182
    *        `-- C        |
183
    *             <-------²
184
    *
185
    *        D is moved from B to C
186
    *
187
    * @param dr   d is readable 0|1
188
    * @param br   b is readable 0|1
189
    * @param cr   c is readable 0|1
190
    * @param lb   b is listened 0|1
191
    * @param lc   c is listened 0|1
192
    * @param ld   d is listened 0|1
193
    * @param res  expected result: item | from | from_wo_c | to | to_wo_b | none
194
    * @param msg  message to display if the test fail
195
    */
196
    function _runTest($dr, $br, $cr, $lb, $lc, $ld, $res, $msg = "%s") {
197
        $msg = "[$dr, $br, $cr, $lb, $lc, $ld, $res] ". $msg;
198
199
        $a =& new MockDocman_Item();
200
        $a->setReturnValue('getId', 'a');
201
        $a->setReturnValue('getParentId', 0);
202
        $b =& new MockDocman_Item();
203
        $b->setReturnValue('getId', 'b');
204
        $b->setReturnValue('getParentId', 'a');
205
        $c =& new MockDocman_Item();
206
        $c->setReturnValue('getId', 'c');
207
        $c->setReturnValue('getParentId', 'a');
208
        $d =& new MockDocman_Item();
209
        $d->setReturnValue('getId', 'd');
210
        $d->setReturnValue('getParentId', 'b');
211
212
        $group_id = 101;
213
        $project  = aMockProject()->withId($group_id)->build();
214
215
        $user = mock('PFUser');
216
        $user->setReturnValue('getId', 'user');
217
        $listener = mock('PFUser');
218
        $listener->setReturnValue('getId', 'listener');
219
220
        $feedback =& new Feedback();
221
222
        $item_factory =& new MockDocman_ItemFactory();
223
        $item_factory->setReturnReference('getItemFromDb', $a, array($a->getId()));
224
        $item_factory->setReturnReference('getItemFromDb', $b, array($b->getId()));
225
        $item_factory->setReturnReference('getItemFromDb', $c, array($c->getId()));
226
        $item_factory->setReturnReference('getItemFromDb', $d, array($d->getId()));
227
228
        $user_manager =& new MockUserManager();
229
        $user_manager->setReturnReference('getUserById', $user, array($user->getId()));
230
        $user_manager->setReturnReference('getUserById', $listener, array($listener->getId()));
231
232
        $permissions_manager =& new MockDocman_PermissionsManager();
233
        $permissions_manager->setReturnValue('userCanRead',   true, array(&$listener, $a->getId()));
234
        $permissions_manager->setReturnValue('userCanAccess', true, array(&$listener, $a->getId()));
235
        $permissions_manager->setReturnValue('userCanRead',   $cr, array(&$listener, $c->getId()));
236
        $permissions_manager->setReturnValue('userCanAccess', $cr, array(&$listener, $c->getId()));
237
        $permissions_manager->setReturnValue('userCanRead',   $br, array(&$listener, $b->getId()));
238
        $permissions_manager->setReturnValue('userCanAccess', $br, array(&$listener, $b->getId()));
239
        $permissions_manager->setReturnValue('userCanRead',   $dr, array(&$listener, $d->getId()));
240
        $permissions_manager->setReturnValue('userCanAccess', $dr && $br, array(&$listener, $d->getId()));
241
242
        $dar_d =& new MockDataAccessResult();
243
        if ($ld) {
244
            $dar_d->setReturnValueAt(0, 'valid', true);
245
            $dar_d->setReturnValueAt(1, 'valid', false);
246
            $dar_d->setReturnValue('current', array('user_id' => $listener->getId(), 'object_id' => $d->getId()));
247
        } else {
248
            $dar_d->setReturnValue('valid', false);
249
        }
250
251
        $dar_c =& new MockDataAccessResult();
252
        if ($lc) {
253
            $dar_c->setReturnValueAt(0, 'valid', true);
254
            $dar_c->setReturnValueAt(1, 'valid', false);
255
            $dar_c->setReturnValue('current', array('user_id' => $listener->getId(), 'object_id' => $c->getId()));
256
        } else {
257
            $dar_c->setReturnValue('valid', false);
258
        }
259
260
        $dar_b =& new MockDataAccessResult();
261
        if ($lb) {
262
            $dar_b->setReturnValueAt(0, 'valid', true);
263
            $dar_b->setReturnValueAt(1, 'valid', false);
264
            $dar_b->setReturnValue('current', array('user_id' => $listener->getId(), 'object_id' => $b->getId()));
265
        } else {
266
            $dar_b->setReturnValue('valid', false);
267
        }
268
269
        $docman_path =& new MockDocman_Path();
270
271
        $dao =& new MockNotificationsDao();
272
        $dao->setReturnValue('searchUserIdByObjectIdAndType', $dar_d, array($d->getId(), 'plugin_docman'));
273
        $dao->setReturnValue('searchUserIdByObjectIdAndType', $dar_c, array($c->getId(), 'plugin_docman'));
274
        $dao->setReturnValue('searchUserIdByObjectIdAndType', $dar_b, array($b->getId(), 'plugin_docman'));
275
276
        $dnmm =& new Docman_NotificationsManager_MoveTestVersion();
277
        $dnmm->setReturnReference('_groupGetObject', $project);
278
        $dnmm->setReturnReference('_getItemFactory', $item_factory);
279
        $dnmm->setReturnReference('_getUserManager', $user_manager);
280
        $dnmm->setReturnReference('_getPermissionsManager', $permissions_manager);
281
        $dnmm->setReturnReference('_getDocmanPath', $docman_path);
282
        $dnmm->setReturnReference('_getDao', $dao);
283
284
        if ($res != 'none') {
285
            $dnmm->expectOnce('_buildMessage', false, $msg);
286
        } else {
287
            $dnmm->expectNever('_buildMessage', $msg);
288
        }
289
290
        $mail_builder = new MailBuilder(TemplateRendererFactory::build());
291
292
        //C'est parti
293
        $dnmm->__construct($project, 'my_url', $feedback, $mail_builder);
294
        $dnmm->somethingHappen('plugin_docman_event_move', array(
295
            'group_id' => $group_id,
296
            'item'    => &$d,
297
            'parent'  => &$c,
298
            'user'    => &$user)
299
        );
300
301
    }
302
}
303