]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - tests/ActivityGenerationTests.php
Replace common_good_random with common_random_hexstr
[quix0rs-gnu-social.git] / tests / ActivityGenerationTests.php
1 <?php
2
3 if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
4     print "This script must be run from the command line\n";
5     exit();
6 }
7
8 // XXX: we should probably have some common source for this stuff
9
10 define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
11 define('GNUSOCIAL', true);
12 define('STATUSNET', true);  // compatibility
13
14 require_once INSTALLDIR . '/lib/common.php';
15
16 class ActivityGenerationTests extends PHPUnit_Framework_TestCase
17 {
18     var $author1 = null;
19     var $author2 = null;
20
21     var $targetUser1 = null;
22     var $targetUser2 = null;
23
24     var $targetGroup1 = null;
25     var $targetGroup2 = null;
26
27     function __construct()
28     {
29         parent::__construct();
30
31         $authorNick1 = 'activitygenerationtestsuser' . common_random_hexstr(4);
32         $authorNick2 = 'activitygenerationtestsuser' . common_random_hexstr(4);
33
34         $targetNick1 = 'activitygenerationteststarget' . common_random_hexstr(4);
35         $targetNick2 = 'activitygenerationteststarget' . common_random_hexstr(4);
36
37         $groupNick1 = 'activitygenerationtestsgroup' . common_random_hexstr(4);
38         $groupNick2 = 'activitygenerationtestsgroup' . common_random_hexstr(4);
39
40         $this->author1 = User::register(array('nickname' => $authorNick1,
41                                               'email' => $authorNick1 . '@example.net',
42                                               'email_confirmed' => true));
43
44         $this->author2 = User::register(array('nickname' => $authorNick2,
45                                               'email' => $authorNick2 . '@example.net',
46                                               'email_confirmed' => true));
47
48         $this->targetUser1 = User::register(array('nickname' => $targetNick1,
49                                                   'email' => $targetNick1 . '@example.net',
50                                                   'email_confirmed' => true));
51
52         $this->targetUser2 = User::register(array('nickname' => $targetNick2,
53                                                   'email' => $targetNick2 . '@example.net',
54                                                   'email_confirmed' => true));
55
56         $this->targetGroup1 = User_group::register(array('nickname' => $groupNick1,
57                                                          'userid' => $this->author1->id,
58                                                          'aliases' => array(),
59                                                          'local' => true,
60                                                          'location' => null,
61                                                          'description' => null,
62                                                          'fullname' => null,
63                                                          'homepage' => null,
64                                                          'mainpage' => null));
65         $this->targetGroup2 = User_group::register(array('nickname' => $groupNick2,
66                                                          'userid' => $this->author1->id,
67                                                          'aliases' => array(),
68                                                          'local' => true,
69                                                          'location' => null,
70                                                          'description' => null,
71                                                          'fullname' => null,
72                                                          'homepage' => null,
73                                                          'mainpage' => null));
74     }
75
76     public function testBasicNoticeActivity()
77     {
78         $notice = $this->_fakeNotice();
79
80         $entry = $notice->asAtomEntry(true);
81
82         $element = $this->_entryToElement($entry, false);
83
84         $this->assertEquals($notice->uri, ActivityUtils::childContent($element, 'id'));
85         $this->assertEquals($notice->content, ActivityUtils::childContent($element, 'title'));
86         $this->assertEquals($notice->rendered, ActivityUtils::childContent($element, 'content'));
87         $this->assertEquals(strtotime($notice->created), strtotime(ActivityUtils::childContent($element, 'published')));
88         $this->assertEquals(strtotime($notice->created), strtotime(ActivityUtils::childContent($element, 'updated')));
89         $this->assertEquals(ActivityVerb::POST, ActivityUtils::childContent($element, 'verb', Activity::SPEC));
90         $this->assertEquals(ActivityObject::NOTE, ActivityUtils::childContent($element, 'object-type', Activity::SPEC));
91     }
92
93     public function testNamespaceFlag()
94     {
95         $notice = $this->_fakeNotice();
96
97         $entry = $notice->asAtomEntry(true);
98
99         $element = $this->_entryToElement($entry, false);
100
101         $this->assertTrue($element->hasAttribute('xmlns'));
102         $this->assertTrue($element->hasAttribute('xmlns:thr'));
103         $this->assertTrue($element->hasAttribute('xmlns:georss'));
104         $this->assertTrue($element->hasAttribute('xmlns:activity'));
105         $this->assertTrue($element->hasAttribute('xmlns:media'));
106         $this->assertTrue($element->hasAttribute('xmlns:poco'));
107         $this->assertTrue($element->hasAttribute('xmlns:ostatus'));
108         $this->assertTrue($element->hasAttribute('xmlns:statusnet'));
109
110         $entry = $notice->asAtomEntry(false);
111
112         $element = $this->_entryToElement($entry, true);
113
114         $this->assertFalse($element->hasAttribute('xmlns'));
115         $this->assertFalse($element->hasAttribute('xmlns:thr'));
116         $this->assertFalse($element->hasAttribute('xmlns:georss'));
117         $this->assertFalse($element->hasAttribute('xmlns:activity'));
118         $this->assertFalse($element->hasAttribute('xmlns:media'));
119         $this->assertFalse($element->hasAttribute('xmlns:poco'));
120         $this->assertFalse($element->hasAttribute('xmlns:ostatus'));
121         $this->assertFalse($element->hasAttribute('xmlns:statusnet'));
122     }
123
124     public function testSourceFlag()
125     {
126         $notice = $this->_fakeNotice();
127
128         // Test with no source
129
130         $entry = $notice->asAtomEntry(false, false);
131
132         $element = $this->_entryToElement($entry, true);
133
134         $source = ActivityUtils::child($element, 'source');
135
136         $this->assertNull($source);
137
138         // Test with source
139
140         $entry = $notice->asAtomEntry(false, true);
141
142         $element = $this->_entryToElement($entry, true);
143
144         $source = ActivityUtils::child($element, 'source');
145
146         $this->assertNotNull($source);
147     }
148
149     public function testSourceContent()
150     {
151         $notice = $this->_fakeNotice();
152         // make a time difference!
153         sleep(2);
154         $notice2 = $this->_fakeNotice();
155
156         $entry = $notice->asAtomEntry(false, true);
157
158         $element = $this->_entryToElement($entry, true);
159
160         $source = ActivityUtils::child($element, 'source');
161
162         $atomUrl = common_local_url('ApiTimelineUser', array('id' => $this->author1->id, 'format' => 'atom'));
163
164         $profile = $this->author1->getProfile();
165
166         $this->assertEquals($atomUrl, ActivityUtils::childContent($source, 'id'));
167         $this->assertEquals($atomUrl, ActivityUtils::getLink($source, 'self', 'application/atom+xml'));
168         $this->assertEquals($profile->profileurl, ActivityUtils::getPermalink($source));
169         $this->assertEquals(strtotime($notice2->created), strtotime(ActivityUtils::childContent($source, 'updated')));
170         // XXX: do we care here?
171         $this->assertFalse(is_null(ActivityUtils::childContent($source, 'title')));
172         $this->assertEquals(common_config('license', 'url'), ActivityUtils::getLink($source, 'license'));
173     }
174
175     public function testAuthorFlag()
176     {
177         $notice = $this->_fakeNotice();
178
179         // Test with no author
180
181         $entry = $notice->asAtomEntry(false, false, false);
182
183         $element = $this->_entryToElement($entry, true);
184
185         $this->assertNull(ActivityUtils::child($element, 'author'));
186         $this->assertNull(ActivityUtils::child($element, 'actor', Activity::SPEC));
187
188         // Test with source
189
190         $entry = $notice->asAtomEntry(false, false, true);
191
192         $element = $this->_entryToElement($entry, true);
193
194         $author = ActivityUtils::child($element, 'author');
195         $actor  = ActivityUtils::child($element, 'actor', Activity::SPEC);
196
197         $this->assertFalse(is_null($author));
198         $this->assertTrue(is_null($actor)); // <activity:actor> is obsolete, no longer added
199     }
200
201     public function testAuthorContent()
202     {
203         $notice = $this->_fakeNotice();
204
205         // Test with author
206
207         $entry = $notice->asAtomEntry(false, false, true);
208
209         $element = $this->_entryToElement($entry, true);
210
211         $author = ActivityUtils::child($element, 'author');
212
213         $this->assertEquals($this->author1->nickname, ActivityUtils::childContent($author, 'name'));
214         $this->assertEquals($this->author1->uri, ActivityUtils::childContent($author, 'uri'));
215     }
216
217     /**
218      * We no longer create <activity:actor> entries, they have merged to <atom:author>
219      */
220     public function testActorContent()
221     {
222         $notice = $this->_fakeNotice();
223
224         // Test with author
225
226         $entry = $notice->asAtomEntry(false, false, true);
227
228         $element = $this->_entryToElement($entry, true);
229
230         $actor = ActivityUtils::child($element, 'actor', Activity::SPEC);
231
232         $this->assertEquals($actor, null);
233     }
234
235     public function testReplyLink()
236     {
237         $orig = $this->_fakeNotice($this->targetUser1);
238
239         $text = "@" . $this->targetUser1->nickname . " reply text " . common_random_hexstr(4);
240
241         $reply = Notice::saveNew($this->author1->id, $text, 'test', array('uri' => null, 'reply_to' => $orig->id));
242
243         $entry = $reply->asAtomEntry();
244
245         $element = $this->_entryToElement($entry, true);
246
247         $irt = ActivityUtils::child($element, 'in-reply-to', 'http://purl.org/syndication/thread/1.0');
248
249         $this->assertNotNull($irt);
250         $this->assertEquals($orig->uri, $irt->getAttribute('ref'));
251         $this->assertEquals($orig->bestUrl(), $irt->getAttribute('href'));
252     }
253
254     public function testReplyAttention()
255     {
256         $orig = $this->_fakeNotice($this->targetUser1);
257
258         $text = "@" . $this->targetUser1->nickname . " reply text " . common_random_hexstr(4);
259
260         $reply = Notice::saveNew($this->author1->id, $text, 'test', array('uri' => null, 'reply_to' => $orig->id));
261
262         $entry = $reply->asAtomEntry();
263
264         $element = $this->_entryToElement($entry, true);
265
266         $this->assertEquals($this->targetUser1->uri, ActivityUtils::getLink($element, 'ostatus:attention'));
267         $this->assertEquals($this->targetUser1->uri, ActivityUtils::getLink($element, 'mentioned'));
268     }
269
270     public function testMultipleReplyAttention()
271     {
272         $orig = $this->_fakeNotice($this->targetUser1);
273
274         $text = "@" . $this->targetUser1->nickname . " reply text " . common_random_hexstr(4);
275
276         $reply = Notice::saveNew($this->targetUser2->id, $text, 'test', array('uri' => null, 'reply_to' => $orig->id));
277
278         $text = "@" . $this->targetUser1->nickname . " @" . $this->targetUser2->nickname . " reply text " . common_random_hexstr(4);
279
280         $reply2 = Notice::saveNew($this->author1->id, $text, 'test', array('uri' => null, 'reply_to' => $reply->id));
281
282         $entry = $reply2->asAtomEntry();
283
284         $element = $this->_entryToElement($entry, true);
285
286         $links = ActivityUtils::getLinks($element, 'ostatus:attention');
287
288         $this->assertEquals(2, count($links));
289
290         $hrefs = array();
291
292         foreach ($links as $link) {
293             $hrefs[] = $link->getAttribute('href');
294         }
295
296         $this->assertTrue(in_array($this->targetUser1->uri, $hrefs));
297         $this->assertTrue(in_array($this->targetUser2->uri, $hrefs));
298
299         $links = ActivityUtils::getLinks($element, 'mentioned');
300
301         $this->assertEquals(2, count($links));
302
303         $hrefs = array();
304
305         foreach ($links as $link) {
306             $hrefs[] = $link->getAttribute('href');
307         }
308
309         $this->assertTrue(in_array($this->targetUser1->uri, $hrefs));
310         $this->assertTrue(in_array($this->targetUser2->uri, $hrefs));
311     }
312
313     public function testGroupPostAttention()
314     {
315         $text = "!" . $this->targetGroup1->nickname . " reply text " . common_random_hexstr(4);
316
317         $notice = Notice::saveNew($this->author1->id, $text, 'test', array('uri' => null));
318
319         $entry = $notice->asAtomEntry();
320
321         $element = $this->_entryToElement($entry, true);
322
323         $this->assertEquals($this->targetGroup1->uri, ActivityUtils::getLink($element, 'ostatus:attention'));
324         $this->assertEquals($this->targetGroup1->uri, ActivityUtils::getLink($element, 'mentioned'));
325     }
326
327     public function testMultipleGroupPostAttention()
328     {
329         $text = "!" . $this->targetGroup1->nickname . " !" . $this->targetGroup2->nickname . " reply text " . common_random_hexstr(4);
330
331         $notice = Notice::saveNew($this->author1->id, $text, 'test', array('uri' => null));
332
333         $entry = $notice->asAtomEntry();
334
335         $element = $this->_entryToElement($entry, true);
336
337         $links = ActivityUtils::getLinks($element, 'ostatus:attention');
338
339         $this->assertEquals(2, count($links));
340
341         $hrefs = array();
342
343         foreach ($links as $link) {
344             $hrefs[] = $link->getAttribute('href');
345         }
346
347         $this->assertTrue(in_array($this->targetGroup1->uri, $hrefs));
348         $this->assertTrue(in_array($this->targetGroup2->uri, $hrefs));
349
350         $links = ActivityUtils::getLinks($element, 'mentioned');
351
352         $this->assertEquals(2, count($links));
353
354         $hrefs = array();
355
356         foreach ($links as $link) {
357             $hrefs[] = $link->getAttribute('href');
358         }
359
360         $this->assertTrue(in_array($this->targetGroup1->uri, $hrefs));
361         $this->assertTrue(in_array($this->targetGroup2->uri, $hrefs));
362     }
363
364     public function testRepeatLink()
365     {
366         $notice = $this->_fakeNotice($this->author1);
367         $repeat = $notice->repeat($this->author2->id, 'test');
368
369         $entry = $repeat->asAtomEntry();
370
371         $element = $this->_entryToElement($entry, true);
372
373         $forward = ActivityUtils::child($element, 'forward', "http://ostatus.org/schema/1.0");
374
375         $this->assertNotNull($forward);
376         $this->assertEquals($notice->uri, $forward->getAttribute('ref'));
377         $this->assertEquals($notice->bestUrl(), $forward->getAttribute('href'));
378     }
379
380     public function testTag()
381     {
382         $tag1 = common_random_hexstr(4);
383
384         $notice = $this->_fakeNotice($this->author1, '#' . $tag1);
385
386         $entry = $notice->asAtomEntry();
387
388         $element = $this->_entryToElement($entry, true);
389
390         $category = ActivityUtils::child($element, 'category');
391
392         $this->assertNotNull($category);
393         $this->assertEquals($tag1, $category->getAttribute('term'));
394     }
395
396     public function testMultiTag()
397     {
398         $tag1 = common_random_hexstr(4);
399         $tag2 = common_random_hexstr(4);
400
401         $notice = $this->_fakeNotice($this->author1, '#' . $tag1 . ' #' . $tag2);
402
403         $entry = $notice->asAtomEntry();
404
405         $element = $this->_entryToElement($entry, true);
406
407         $categories = $element->getElementsByTagName('category');
408
409         $this->assertNotNull($categories);
410         $this->assertEquals(2, $categories->length);
411
412         $terms = array();
413
414         for ($i = 0; $i < $categories->length; $i++) {
415             $cat = $categories->item($i);
416             $terms[] = $cat->getAttribute('term');
417         }
418
419         $this->assertTrue(in_array($tag1, $terms));
420         $this->assertTrue(in_array($tag2, $terms));
421     }
422
423     public function testGeotaggedActivity()
424     {
425         $notice = Notice::saveNew($this->author1->id, common_random_hexstr(4), 'test', array('uri' => null, 'lat' => 45.5, 'lon' => -73.6));
426
427         $entry = $notice->asAtomEntry();
428
429         $element = $this->_entryToElement($entry, true);
430
431         $this->assertEquals('45.5 -73.6', ActivityUtils::childContent($element, 'point', "http://www.georss.org/georss"));
432     }
433
434     public function testNoticeInfo()
435     {
436         $notice = $this->_fakeNotice();
437
438         $entry = $notice->asAtomEntry();
439
440         $element = $this->_entryToElement($entry, true);
441
442         $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");
443
444         $this->assertEquals($notice->id, $noticeInfo->getAttribute('local_id'));
445         $this->assertEquals($notice->source, $noticeInfo->getAttribute('source'));
446         $this->assertEquals('', $noticeInfo->getAttribute('repeat_of'));
447         $this->assertEquals('', $noticeInfo->getAttribute('repeated'));
448         $this->assertEquals('', $noticeInfo->getAttribute('favorite'));
449         $this->assertEquals('', $noticeInfo->getAttribute('source_link'));
450     }
451
452     public function testNoticeInfoRepeatOf()
453     {
454         $notice = $this->_fakeNotice();
455
456         $repeat = $notice->repeat($this->author2->id, 'test');
457
458         $entry = $repeat->asAtomEntry();
459
460         $element = $this->_entryToElement($entry, true);
461
462         $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");
463
464         $this->assertEquals($notice->id, $noticeInfo->getAttribute('repeat_of'));
465     }
466
467     public function testNoticeInfoRepeated()
468     {
469         $notice = $this->_fakeNotice();
470
471         $repeat = $notice->repeat($this->author2->id, 'test');
472
473         $entry = $notice->asAtomEntry(false, false, false, $this->author2);
474
475         $element = $this->_entryToElement($entry, true);
476
477         $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");
478
479         $this->assertEquals('true', $noticeInfo->getAttribute('repeated'));
480
481         $entry = $notice->asAtomEntry(false, false, false, $this->targetUser1);
482
483         $element = $this->_entryToElement($entry, true);
484
485         $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");
486
487         $this->assertEquals('false', $noticeInfo->getAttribute('repeated'));
488     }
489
490     public function testNoticeInfoFave()
491     {
492         $notice = $this->_fakeNotice();
493
494         $fave = Fave::addNew($this->author2->getProfile(), $notice);
495
496         // Should be set if user has faved
497
498         $entry = $notice->asAtomEntry(false, false, false, $this->author2);
499
500         $element = $this->_entryToElement($entry, true);
501
502         $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");
503
504         $this->assertEquals('true', $noticeInfo->getAttribute('favorite'));
505
506         // Shouldn't be set if user has not faved
507
508         $entry = $notice->asAtomEntry(false, false, false, $this->targetUser1);
509
510         $element = $this->_entryToElement($entry, true);
511
512         $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");
513
514         $this->assertEquals('false', $noticeInfo->getAttribute('favorite'));
515     }
516
517     public function testConversationLink()
518     {
519         $orig = $this->_fakeNotice($this->targetUser1);
520
521         $text = "@" . $this->targetUser1->nickname . " reply text " . common_random_hexstr(4);
522
523         $reply = Notice::saveNew($this->author1->id, $text, 'test', array('uri' => null, 'reply_to' => $orig->id));
524
525         $conv = Conversation::getKV('id', $reply->conversation);
526
527         $entry = $reply->asAtomEntry();
528
529         $element = $this->_entryToElement($entry, true);
530
531         $this->assertEquals($conv->uri, ActivityUtils::getLink($element, 'ostatus:conversation'));
532     }
533
534     function __destruct()
535     {
536         if (!is_null($this->author1)) {
537             $this->author1->delete();
538         }
539
540         if (!is_null($this->author2)) {
541             $this->author2->delete();
542         }
543
544         if (!is_null($this->targetUser1)) {
545             $this->targetUser1->delete();
546         }
547
548         if (!is_null($this->targetUser2)) {
549             $this->targetUser2->delete();
550         }
551
552         if (!is_null($this->targetGroup1)) {
553             $this->targetGroup1->delete();
554         }
555
556         if (!is_null($this->targetGroup2)) {
557             $this->targetGroup2->delete();
558         }
559     }
560
561     private function _fakeNotice($user = null, $text = null)
562     {
563         if (empty($user)) {
564             $user = $this->author1;
565         }
566
567         if (empty($text)) {
568             $text = "fake-o text-o " . common_random_hexstr(32);
569         }
570
571         return Notice::saveNew($user->id, $text, 'test', array('uri' => null));
572     }
573
574     private function _entryToElement($entry, $namespace = false)
575     {
576         $xml = '<?xml version="1.0" encoding="utf-8"?>'."\n\n";
577         $xml .= '<feed';
578         if ($namespace) {
579             $xml .= ' xmlns="http://www.w3.org/2005/Atom"';
580             $xml .= ' xmlns:thr="http://purl.org/syndication/thread/1.0"';
581             $xml .= ' xmlns:georss="http://www.georss.org/georss"';
582             $xml .= ' xmlns:activity="http://activitystrea.ms/spec/1.0/"';
583             $xml .= ' xmlns:media="http://purl.org/syndication/atommedia"';
584             $xml .= ' xmlns:poco="http://portablecontacts.net/spec/1.0"';
585             $xml .= ' xmlns:ostatus="http://ostatus.org/schema/1.0"';
586             $xml .= ' xmlns:statusnet="http://status.net/schema/api/1/"';
587         }
588         $xml .= '>' . "\n" . $entry . "\n" . '</feed>' . "\n";
589         $doc = DOMDocument::loadXML($xml);
590         $feed = $doc->documentElement;
591         $entries = $feed->getElementsByTagName('entry');
592
593         return $entries->item(0);
594     }
595 }