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