File: Base.php

package info (click to toggle)
php-horde-content 2.0.6-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 392 kB
  • sloc: php: 1,490; xml: 512; sh: 13; makefile: 9
file content (399 lines) | stat: -rw-r--r-- 14,250 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
115
116
117
118
119
120
121
122
123
124
125
126
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
<?php
/**
 * Copyright 2008-2017 Horde LLC (http://www.horde.org/)
 *
 * @author     Michael J Rubinsky <mrubinsk@horde.org>
 * @author     Chuck Hagenbuch <chuck@horde.org>
 * @category   Horde
 * @package    Content
 * @subpackage UnitTests
 */
class Content_Test_Base extends Horde_Test_Case
{
    /**
     * @static Content_Tagger
     */
    static $tagger;
    static $type_mgr;

    /**
     * Primes the fixture, and tests basic tagging functionality where all
     * bits of data are new (user, type, object, tag)..
     *
     */
    protected function _create()
    {

        $this->_testEmpty();

        // user alice tags an event named 'party' with the tag 'personal' and
        // an event named 'anniversary' with the tag 'personal'
        self::$tagger->tag('alice', array('type' => 'event', 'object' => 'party'), 'play', new Horde_Date('2008-01-01T00:10:00'));

        // user alice tags an event named 'office hours' with the tag 'work'
        self::$tagger->tag('alice', array('type' => 'event', 'object' => 'office hours'), 'work', new Horde_Date('2008-01-01T00:05:00'));

        // user bob tags a blog named 'daring fireball' with the tag 'apple'
        self::$tagger->tag('bob', array('type' => 'blog', 'object' => 'daring fireball'), 'apple', new Horde_Date('2008-01-01T00:20:00'));

        // Two users have tagged the same object, with the same tag
        self::$tagger->tag('alice', array('type' => 'event', 'object' => 'anniversary'), 'personal', new Horde_Date('2009-01-01T00:05:00'));
        self::$tagger->tag('bob', array('type' => 'event', 'object' => 'anniversary'), 'personal', new Horde_Date('2009-01-01T00:06:00'));
    }

    protected function _testEmpty()
    {
        // Basic check that no data exists.
        $this->assertEmpty(self::$tagger->getTags(array()));
        $this->assertEmpty(self::$tagger->getRecentTags());
        $this->assertEmpty(self::$tagger->getRecentObjects());
    }

    /**
     * Test types
     *
     */
    protected function _testEnsureTypes()
    {
        $this->assertEquals(array(0 => 1, 1 => 2), self::$type_mgr->ensureTypes(array('event', 'blog')));
        $this->assertEquals(array(0 => 2, 1 => 1), self::$type_mgr->ensureTypes(array('blog', 'event')));
        $this->assertEquals(array(0 => 3, 1 => 2), self::$type_mgr->ensureTypes(array('foo', 'blog')));
    }

    /**
     * Test ensureTags.
     *
     * 1 => play
     * 2 => work
     * 3 => apple
     * 4 => personal
     */
    protected function _testEnsureTags()
    {
        // Test passing tag_ids to ensureTags
        $this->assertEquals(array(1 => 1), self::$tagger->ensureTags(1));
        $this->assertEquals(array(1 => 1), self::$tagger->ensureTags(array(1)));
        $this->assertEquals(array(1 => 1, 2 => 2), self::$tagger->ensureTags(array(1, 2)));

        // Test passing tag names
        $this->assertEquals(array('work' => 2), self::$tagger->ensureTags('work'));
        $this->assertEquals(array('work' => 2), self::$tagger->ensureTags(array('work')));
        $this->assertEquals(array('work' => 2, 'play' => 1), self::$tagger->ensureTags(array('work', 'play')));

        // Test mixed
        $this->assertEquals(array(1 => 1, 'play' => 1), self::$tagger->ensureTags(array(1, 'play')));
        $this->assertEquals(array(2 => 2, 'work' => 2), self::$tagger->ensureTags(array('work', 2)));
        $this->assertEquals(array(1 => 1, 'work' => 2), self::$tagger->ensureTags(array(1, 'work')));
    }

    protected function _testFullTagCloudSimple()
    {
        $expected = array(
            '1' => array(
                'tag_id' => 1,
                'tag_name' => 'play',
                'count' => 1
            ),

            '2' => array(
                'tag_id' => 2,
                'tag_name' => 'work',
                'count' => 1
            ),

            '3' => array(
                'tag_id' => 3,
                'tag_name' => 'apple',
                'count' => 1
            ),

            '4' => array(
                'tag_id' => 4,
                'tag_name' => 'personal',
                'count' => 2
            )
        );

        $cloud = self::$tagger->getTagCloud();
        $this->assertEquals($expected, $cloud);
    }

    protected function _testTagCloudByType()
    {
        $expected = array(
            '3' => array(
                'tag_id' => 3,
                'tag_name' => 'apple',
                'count' => 1
            )
        );
        $cloud = self::$tagger->getTagCloud(array('typeId' => 'blog'));
        $this->assertEquals($expected, $cloud);
    }

    protected function _testTagCloudByUser()
    {
        $expected = array(
            '3' => array(
                'tag_id' => 3,
                'tag_name' => 'apple',
                'count' => 1
            ),
            '4' => array(
                'tag_id' => 4,
                'tag_name' => 'personal',
                'count' => 1
            )
        );
        $cloud = self::$tagger->getTagCloud(array('userId' => 'bob'));
        $this->assertEquals($expected, $cloud);
    }

    protected function _testTagCloudByUserType()
    {
        $expected = array(
            '1' => array(
                'tag_id' => 1,
                'tag_name' => 'play',
                'count' => 1
            ),
            '2' => array(
                'tag_id' => 2,
                'tag_name' => 'work',
                'count' => 1
            ),
            '4' => array(
                'tag_id' => 4,
                'tag_name' => 'personal',
                'count' => 1
            )
        );
        $cloud = self::$tagger->getTagCloud(array('userId' => 'alice', 'typeId' => 'event'));
        $this->assertEquals($expected, $cloud);
    }

    protected function _testTagCloudByTagType()
    {
        $expected = array(
            '2' => array(
                'tag_id' => 2,
                'tag_name' => 'work',
                'count' => 1
            )
        );
        $cloud = self::$tagger->getTagCloud(array('tagIds' => array(2), 'typeId' => 'event'));
        $this->assertEquals($expected, $cloud);
    }

    protected function _testTagCloudByTagIds()
    {
        $expected = array(
            '2' => array(
                'tag_id' => 2,
                'tag_name' => 'work',
                'count' => 1
            ),
            '4' => array(
                'tag_id' => 4,
                'tag_name' => 'personal',
                'count' => 2
            )
        );
        $cloud = self::$tagger->getTagCloud(array('tagIds' => array(2, 4)));
        $this->assertEquals($expected, $cloud);
    }

    protected function _testGetRecentTags()
    {
        $recent = self::$tagger->getRecentTags();
        $this->assertEquals(4, count($recent));
        $this->assertEquals(4, $recent[0]['tag_id']);
        $this->assertEquals('personal', $recent[0]['tag_name']);
        $date = new Horde_Date($recent[0]['created']);
        // date --date="2009-01-01 00:06:00 UTC" +%s gives: 1230768360
        $this->assertEquals(1230768360, $date->timestamp());
    }

    protected function _testGetRecentTagsByUser()
    {
        $recent = self::$tagger->getRecentTags(array('userId' => 1));
        $this->assertEquals(3, count($recent));

        $recent = self::$tagger->getRecentTags(array('userId' => 2));
        $this->assertEquals(2, count($recent));

        $recent = self::$tagger->getRecentTags(array('userId' => 'alice'));
        $this->assertEquals(3, count($recent));
    }

    protected function _testGetRecentTagsByType()
    {
        $recent = self::$tagger->getRecentTags(array('typeId' => 'event'));
        $this->assertEquals(3, count($recent));
    }

    protected function _testGetRecentObjects()
    {
        $recent = self::$tagger->getRecentObjects();
        $this->assertEquals(4, count($recent));
        $this->assertEquals(4, $recent[0]['object_id']);
        $date = new Horde_Date($recent[0]['created'], 'UTC');
        // date --date="2009-01-01 00:06:00 UTC" +%s gives: 1230768360
        $this->assertEquals(1230768360, $date->timestamp());
    }

    protected function _testUntag()
    {
        self::$tagger->untag('alice', array('type' => 'event', 'object' => 'party'), 'play');
        $count = self::$tagger->getRecentTags();
        $this->assertEquals(3, count($count));

        //readd
        self::$tagger->tag('alice', array('type' => 'event', 'object' => 'party'), 'play', new Horde_Date('2008-01-01T00:10:00'));
        $count = self::$tagger->getRecentTags();
        $this->assertEquals(4, count($count));
    }
    /**
     * @TODO: SHould validate the values too, not just the count.
     */
    protected function _testGetRecentObjectsByUser()
    {
        // alice has 3 recent objects
        $recent = self::$tagger->getRecentObjects(array('userId' => 'alice'));
        $this->assertEquals(3, count($recent));

        // bob has 2
        $recent = self::$tagger->getRecentObjects(array('userId' => 'bob'));
        $this->assertEquals(2, count($recent));

        // just for kicks, test using the user id, not name.
        $recent = self::$tagger->getRecentObjects(array('userId' => 1));
        $this->assertEquals(3, count($recent));
    }

    protected function _testGetRecentObjectsByType()
    {
        $recent = self::$tagger->getRecentObjects(array('typeId' => 1));
        $this->assertEquals(3, count($recent));

        $recent = self::$tagger->getRecentObjects(array('typeId' => 2));
        $this->assertEquals(1, count($recent));
    }

    protected function _testGetRecentUsers()
    {
        $recent = self::$tagger->getRecentUsers();
        $this->assertEquals(2, count($recent));
    }

    protected function _testGetRecentUsersByType()
    {
        $recent = self::$tagger->getRecentUsers(array('typeId' => 1));
        $this->assertEquals(2, count($recent));

        $recent = self::$tagger->getRecentUsers(array('typeId' => 2));
        $this->assertEquals(1, count($recent));
    }

    /**
     * Test obtaining objects that are tagged with the same tags as the provided
     * object.
     *
     * See Bug: 10439
     */
    public function testGetObjectsByObjectId()
    {
        self::$tagger->tag('mike', array('type' => 'event', 'object' => 'irene'), 'hurricane', new Horde_Date('2011-08-28T00:01:00'));
        self::$tagger->tag('mike', array('type' => 'event', 'object' => 'floyd'), 'hurricane', new Horde_Date('1999-09-07T00:02:00'));
        $object = self::$tagger->getObjects(array('objectId' => array('type' => 'event', 'object' => 'irene')));
        $this->assertEquals('floyd', current($object));
    }

    public function testDuplicateTagsByCase()
    {
        // These tests don't work at the moment, because SQLite sucks at
        // non-ascii comparing.
        /*
        self::$tagger->tag('mike', 1, 'TYÖ');
        self::$tagger->tag('mike', 1, 'TYÖ');
        self::$tagger->tag('mike', 1, 'työ');
        self::$tagger->tag('mike', 1, 'työ');
        */
        // Use older timestamps to avoid interfering with the later tests
        self::$tagger->tag('mike', array('type' => 'foo', 'object' => 'xyz'), 'foo', new Horde_Date('2008-01-01T00:05:00'));
        self::$tagger->tag('alice', array('type' => 'foo', 'object' => 'xyz'), 'FOO', new Horde_Date('2008-01-01T00:05:00'));
        self::$tagger->tag('alice', array('type' => 'foo', 'object' => 'xyz'), array('test', 'TEST'), new Horde_Date('2008-01-01T00:05:00'));
        $this->assertEquals(2, count(self::$tagger->getTags(array('objectId' => array('type' => 'foo', 'object' => 'xyz')))));
    }

    public function testGetRecentTagsLimit()
    {
        // Create 100 tags on 100 tag_ids, with tag_id = t1 being applied
        // most recently, and so on. Prepend "t" to each tag to force the
        // creation of tags that don't yet exist in the test database.
        for ($i = 1; $i <= 100; $i++) {
            self::$tagger->tag(1, 1, "t$i", new Horde_Date(strtotime('now - ' . $i . ' minutes')));
        }

        $recentLimit = self::$tagger->getRecentTags(array('limit' => 25));
        $this->assertEquals(25, count($recentLimit));
        $this->assertEquals('t1', $recentLimit[0]['tag_name']);
    }

    /**
     * @depends testGetRecentTagsLimit
     */
    public function testGetRecentTagsOffset()
    {
        $recentOffset = self::$tagger->getRecentTags(array('limit' => 25, 'offset' => 25));
        $this->assertEquals(25, count($recentOffset));
        $this->assertEquals('t26', $recentOffset[0]['tag_name']);
    }

    public function testGetRecentObjectsLimit()
    {
        // Create 100 tags on 100 object_ids, with object_id = 1 being tagged
        // most recently, and so on.
        for ($i = 1; $i <= 100; $i++) {
            self::$tagger->tag(1, $i, 1, new Horde_Date(strtotime('now - ' . $i . ' minutes')));
        }

        $recentLimit = self::$tagger->getRecentObjects(array('limit' => 25));
        $this->assertEquals(25, count($recentLimit));
        $this->assertEquals(1, $recentLimit[0]['object_id']);
    }

    /**
     * @depend testGetRecentTagsOffset
     */
    public function testGetRecentObjectsOffset()
    {
        $recentOffset = self::$tagger->getRecentObjects(array('limit' => 25, 'offset' => 25));
        $this->assertEquals(25, count($recentOffset));
        $this->assertEquals(26, $recentOffset[0]['object_id']);
    }

    public function testGetRecentUsersLimit()
    {
        // Create 100 tags by 100 user_ids, with user_id = 1 tagging
        // most recently, and so on.
        for ($i = 1; $i <= 100; $i++) {
            self::$tagger->tag($i, 1, 1, new Horde_Date(strtotime('now - ' . $i . ' minutes')));
        }

        $recentLimit = self::$tagger->getRecentUsers(array('limit' => 25));
        $this->assertEquals(25, count($recentLimit));
        $this->assertEquals(1, $recentLimit[0]['user_id']);
    }

    /**
     * @depend testGetRecentUsersLimit
     */
    public function testGetRecentUsersOffset()
    {
        $recentOffset = self::$tagger->getRecentUsers(array('limit' => 25, 'offset' => 25));
        $this->assertEquals(25, count($recentOffset));
        $this->assertEquals(26, $recentOffset[0]['user_id']);
    }

}