]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - tests/ActivityGenerationTests.php
Use the profile URI when linking instead of URL
[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->getUri(), 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->getNickname(), ActivityUtils::childContent($author, 'name'));
214         $this->assertEquals($this->author1->getUri(), 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->getUri(), $irt->getAttribute('ref'));
251         $this->assertEquals($orig->getUrl(), $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->getUri(), ActivityUtils::getLink($element, 'mentioned'));
267     }
268
269     public function testMultipleReplyAttention()
270     {
271         $orig = $this->_fakeNotice($this->targetUser1);
272
273         $text = "@" . $this->targetUser1->nickname . " reply text " . common_random_hexstr(4);
274
275         $reply = Notice::saveNew($this->targetUser2->id, $text, 'test', array('uri' => null, 'reply_to' => $orig->id));
276
277         $text = "@" . $this->targetUser1->nickname . " @" . $this->targetUser2->nickname . " reply text " . common_random_hexstr(4);
278
279         $reply2 = Notice::saveNew($this->author1->id, $text, 'test', array('uri' => null, 'reply_to' => $reply->id));
280
281         $entry = $reply2->asAtomEntry();
282
283         $element = $this->_entryToElement($entry, true);
284
285         $links = ActivityUtils::getLinks($element, 'mentioned');
286
287         $this->assertEquals(2, count($links));
288
289         $hrefs = array();
290
291         foreach ($links as $link) {
292             $hrefs[] = $link->getAttribute('href');
293         }
294
295         $this->assertTrue(in_array($this->targetUser1->getUri(), $hrefs));
296         $this->assertTrue(in_array($this->targetUser2->getUri(), $hrefs));
297
298         $links = ActivityUtils::getLinks($element, 'mentioned');
299
300         $this->assertEquals(2, count($links));
301
302         $hrefs = array();
303
304         foreach ($links as $link) {
305             $hrefs[] = $link->getAttribute('href');
306         }
307
308         $this->assertTrue(in_array($this->targetUser1->getUri(), $hrefs));
309         $this->assertTrue(in_array($this->targetUser2->getUri(), $hrefs));
310     }
311
312     public function testGroupPostAttention()
313     {
314         $text = "!" . $this->targetGroup1->nickname . " reply text " . common_random_hexstr(4);
315
316         $notice = Notice::saveNew($this->author1->id, $text, 'test', array('uri' => null));
317
318         $entry = $notice->asAtomEntry();
319
320         $element = $this->_entryToElement($entry, true);
321
322         $this->assertEquals($this->targetGroup1->getUri(), ActivityUtils::getLink($element, 'mentioned'));
323     }
324
325     public function testMultipleGroupPostAttention()
326     {
327         $text = "!" . $this->targetGroup1->nickname . " !" . $this->targetGroup2->nickname . " reply text " . common_random_hexstr(4);
328
329         $notice = Notice::saveNew($this->author1->id, $text, 'test', array('uri' => null));
330
331         $entry = $notice->asAtomEntry();
332
333         $element = $this->_entryToElement($entry, true);
334
335         $links = ActivityUtils::getLinks($element, 'mentioned');
336
337         $this->assertEquals(2, count($links));
338
339         $hrefs = array();
340
341         foreach ($links as $link) {
342             $hrefs[] = $link->getAttribute('href');
343         }
344
345         $this->assertTrue(in_array($this->targetGroup1->getUri(), $hrefs));
346         $this->assertTrue(in_array($this->targetGroup2->getUri(), $hrefs));
347
348         $links = ActivityUtils::getLinks($element, 'mentioned');
349
350         $this->assertEquals(2, count($links));
351
352         $hrefs = array();
353
354         foreach ($links as $link) {
355             $hrefs[] = $link->getAttribute('href');
356         }
357
358         $this->assertTrue(in_array($this->targetGroup1->getUri(), $hrefs));
359         $this->assertTrue(in_array($this->targetGroup2->getUri(), $hrefs));
360     }
361
362     public function testRepeatLink()
363     {
364         $notice = $this->_fakeNotice($this->author1);
365         $repeat = $notice->repeat($this->author2->getProfile(), 'test');
366
367         $entry = $repeat->asAtomEntry();
368
369         $element = $this->_entryToElement($entry, true);
370
371         $forward = ActivityUtils::child($element, 'forward', "http://ostatus.org/schema/1.0");
372
373         $this->assertNotNull($forward);
374         $this->assertEquals($notice->getUri(), $forward->getAttribute('ref'));
375         $this->assertEquals($notice->getUrl(), $forward->getAttribute('href'));
376     }
377
378     public function testTag()
379     {
380         $tag1 = common_random_hexstr(4);
381
382         $notice = $this->_fakeNotice($this->author1, '#' . $tag1);
383
384         $entry = $notice->asAtomEntry();
385
386         $element = $this->_entryToElement($entry, true);
387
388         $category = ActivityUtils::child($element, 'category');
389
390         $this->assertNotNull($category);
391         $this->assertEquals($tag1, $category->getAttribute('term'));
392     }
393
394     public function testMultiTag()
395     {
396         $tag1 = common_random_hexstr(4);
397         $tag2 = common_random_hexstr(4);
398
399         $notice = $this->_fakeNotice($this->author1, '#' . $tag1 . ' #' . $tag2);
400
401         $entry = $notice->asAtomEntry();
402
403         $element = $this->_entryToElement($entry, true);
404
405         $categories = $element->getElementsByTagName('category');
406
407         $this->assertNotNull($categories);
408         $this->assertEquals(2, $categories->length);
409
410         $terms = array();
411
412         for ($i = 0; $i < $categories->length; $i++) {
413             $cat = $categories->item($i);
414             $terms[] = $cat->getAttribute('term');
415         }
416
417         $this->assertTrue(in_array($tag1, $terms));
418         $this->assertTrue(in_array($tag2, $terms));
419     }
420
421     public function testGeotaggedActivity()
422     {
423         $notice = Notice::saveNew($this->author1->id, common_random_hexstr(4), 'test', array('uri' => null, 'lat' => 45.5, 'lon' => -73.6));
424
425         $entry = $notice->asAtomEntry();
426
427         $element = $this->_entryToElement($entry, true);
428
429         $this->assertEquals('45.5 -73.6', ActivityUtils::childContent($element, 'point', "http://www.georss.org/georss"));
430     }
431
432     public function testNoticeInfo()
433     {
434         $notice = $this->_fakeNotice();
435
436         $entry = $notice->asAtomEntry();
437
438         $element = $this->_entryToElement($entry, true);
439
440         $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");
441
442         $this->assertEquals($notice->id, $noticeInfo->getAttribute('local_id'));
443         $this->assertEquals($notice->source, $noticeInfo->getAttribute('source'));
444         $this->assertEquals('', $noticeInfo->getAttribute('repeat_of'));
445         $this->assertEquals('', $noticeInfo->getAttribute('repeated'));
446 //        $this->assertEquals('', $noticeInfo->getAttribute('favorite'));
447         $this->assertEquals('', $noticeInfo->getAttribute('source_link'));
448     }
449
450     public function testNoticeInfoRepeatOf()
451     {
452         $notice = $this->_fakeNotice();
453
454         $repeat = $notice->repeat($this->author2->getProfile(), 'test');
455
456         $entry = $repeat->asAtomEntry();
457
458         $element = $this->_entryToElement($entry, true);
459
460         $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");
461
462         $this->assertEquals($notice->id, $noticeInfo->getAttribute('repeat_of'));
463     }
464
465     public function testNoticeInfoRepeated()
466     {
467         $notice = $this->_fakeNotice();
468
469         $repeat = $notice->repeat($this->author2->getProfile(), 'test');
470
471         $entry = $notice->asAtomEntry(false, false, false, $this->author2);
472
473         $element = $this->_entryToElement($entry, true);
474
475         $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");
476
477         $this->assertEquals('true', $noticeInfo->getAttribute('repeated'));
478
479         $entry = $notice->asAtomEntry(false, false, false, $this->targetUser1);
480
481         $element = $this->_entryToElement($entry, true);
482
483         $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");
484
485         $this->assertEquals('false', $noticeInfo->getAttribute('repeated'));
486     }
487
488 /*    public function testNoticeInfoFave()
489     {
490         $notice = $this->_fakeNotice();
491
492         $fave = Fave::addNew($this->author2->getProfile(), $notice);
493
494         // Should be set if user has faved
495
496         $entry = $notice->asAtomEntry(false, false, false, $this->author2);
497
498         $element = $this->_entryToElement($entry, true);
499
500         $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");
501
502         $this->assertEquals('true', $noticeInfo->getAttribute('favorite'));
503
504         // Shouldn't be set if user has not faved
505
506         $entry = $notice->asAtomEntry(false, false, false, $this->targetUser1);
507
508         $element = $this->_entryToElement($entry, true);
509
510         $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");
511
512         $this->assertEquals('false', $noticeInfo->getAttribute('favorite'));
513     }*/
514
515     public function testConversationLink()
516     {
517         $orig = $this->_fakeNotice($this->targetUser1);
518
519         $text = "@" . $this->targetUser1->nickname . " reply text " . common_random_hexstr(4);
520
521         $reply = Notice::saveNew($this->author1->id, $text, 'test', array('uri' => null, 'reply_to' => $orig->id));
522
523         $conv = Conversation::getKV('id', $reply->conversation);
524
525         $entry = $reply->asAtomEntry();
526
527         $element = $this->_entryToElement($entry, true);
528
529         $this->assertEquals($conv->getUri(), ActivityUtils::getLink($element, 'ostatus:conversation'));
530     }
531
532     function __destruct()
533     {
534         if (!is_null($this->author1)) {
535             $this->author1->delete();
536         }
537
538         if (!is_null($this->author2)) {
539             $this->author2->delete();
540         }
541
542         if (!is_null($this->targetUser1)) {
543             $this->targetUser1->delete();
544         }
545
546         if (!is_null($this->targetUser2)) {
547             $this->targetUser2->delete();
548         }
549
550         if (!is_null($this->targetGroup1)) {
551             $this->targetGroup1->delete();
552         }
553
554         if (!is_null($this->targetGroup2)) {
555             $this->targetGroup2->delete();
556         }
557     }
558
559     private function _fakeNotice($user = null, $text = null)
560     {
561         if (empty($user)) {
562             $user = $this->author1;
563         }
564
565         if (empty($text)) {
566             $text = "fake-o text-o " . common_random_hexstr(32);
567         }
568
569         return Notice::saveNew($user->id, $text, 'test', array('uri' => null));
570     }
571
572     private function _entryToElement($entry, $namespace = false)
573     {
574         $xml = '<?xml version="1.0" encoding="utf-8"?>'."\n\n";
575         $xml .= '<feed';
576         if ($namespace) {
577             $xml .= ' xmlns="http://www.w3.org/2005/Atom"';
578             $xml .= ' xmlns:thr="http://purl.org/syndication/thread/1.0"';
579             $xml .= ' xmlns:georss="http://www.georss.org/georss"';
580             $xml .= ' xmlns:activity="http://activitystrea.ms/spec/1.0/"';
581             $xml .= ' xmlns:media="http://purl.org/syndication/atommedia"';
582             $xml .= ' xmlns:poco="http://portablecontacts.net/spec/1.0"';
583             $xml .= ' xmlns:ostatus="http://ostatus.org/schema/1.0"';
584             $xml .= ' xmlns:statusnet="http://status.net/schema/api/1/"';
585         }
586         $xml .= '>' . "\n" . $entry . "\n" . '</feed>' . "\n";
587         $doc = DOMDocument::loadXML($xml);
588         $feed = $doc->documentElement;
589         $entries = $feed->getElementsByTagName('entry');
590
591         return $entries->item(0);
592     }
593 }