]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - tests/ActivityGenerationTests.php
cc82151b99cebc908935b992ab00d3b4da422d8c
[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($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         $actor  = ActivityUtils::child($element, 'actor', Activity::SPEC);
212
213         $this->assertEquals($this->author1->nickname, ActivityUtils::childContent($author, 'name'));
214         $this->assertEquals($this->author1->uri, ActivityUtils::childContent($author, 'uri'));
215     }
216
217     public function testReplyLink()
218     {
219         $orig = $this->_fakeNotice($this->targetUser1);
220
221         $text = "@" . $this->targetUser1->nickname . " reply text " . common_good_rand(4);
222
223         $reply = Notice::saveNew($this->author1->id, $text, 'test', array('uri' => null, 'reply_to' => $orig->id));
224
225         $entry = $reply->asAtomEntry();
226
227         $element = $this->_entryToElement($entry, true);
228
229         $irt = ActivityUtils::child($element, 'in-reply-to', 'http://purl.org/syndication/thread/1.0');
230
231         $this->assertNotNull($irt);
232         $this->assertEquals($orig->uri, $irt->getAttribute('ref'));
233         $this->assertEquals($orig->bestUrl(), $irt->getAttribute('href'));
234     }
235
236     public function testReplyAttention()
237     {
238         $orig = $this->_fakeNotice($this->targetUser1);
239
240         $text = "@" . $this->targetUser1->nickname . " reply text " . common_good_rand(4);
241
242         $reply = Notice::saveNew($this->author1->id, $text, 'test', array('uri' => null, 'reply_to' => $orig->id));
243
244         $entry = $reply->asAtomEntry();
245
246         $element = $this->_entryToElement($entry, true);
247
248         $this->assertEquals($this->targetUser1->uri, ActivityUtils::getLink($element, 'ostatus:attention'));
249     }
250
251     public function testMultipleReplyAttention()
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->targetUser2->id, $text, 'test', array('uri' => null, 'reply_to' => $orig->id));
258
259         $text = "@" . $this->targetUser1->nickname . " @" . $this->targetUser2->nickname . " reply text " . common_good_rand(4);
260
261         $reply2 = Notice::saveNew($this->author1->id, $text, 'test', array('uri' => null, 'reply_to' => $reply->id));
262
263         $entry = $reply2->asAtomEntry();
264
265         $element = $this->_entryToElement($entry, true);
266
267         $links = ActivityUtils::getLinks($element, 'ostatus:attention');
268
269         $this->assertEquals(2, count($links));
270
271         $hrefs = array();
272
273         foreach ($links as $link) {
274             $hrefs[] = $link->getAttribute('href');
275         }
276
277         $this->assertTrue(in_array($this->targetUser1->uri, $hrefs));
278         $this->assertTrue(in_array($this->targetUser2->uri, $hrefs));
279     }
280
281     public function testGroupPostAttention()
282     {
283         $text = "!" . $this->targetGroup1->nickname . " reply text " . common_good_rand(4);
284
285         $notice = Notice::saveNew($this->author1->id, $text, 'test', array('uri' => null));
286
287         $entry = $notice->asAtomEntry();
288
289         $element = $this->_entryToElement($entry, true);
290
291         $this->assertEquals($this->targetGroup1->uri, ActivityUtils::getLink($element, 'ostatus:attention'));
292     }
293
294     public function testMultipleGroupPostAttention()
295     {
296         $text = "!" . $this->targetGroup1->nickname . " !" . $this->targetGroup2->nickname . " reply text " . common_good_rand(4);
297
298         $notice = Notice::saveNew($this->author1->id, $text, 'test', array('uri' => null));
299
300         $entry = $notice->asAtomEntry();
301
302         $element = $this->_entryToElement($entry, true);
303
304         $links = ActivityUtils::getLinks($element, 'ostatus:attention');
305
306         $this->assertEquals(2, count($links));
307
308         $hrefs = array();
309
310         foreach ($links as $link) {
311             $hrefs[] = $link->getAttribute('href');
312         }
313
314         $this->assertTrue(in_array($this->targetGroup1->uri, $hrefs));
315         $this->assertTrue(in_array($this->targetGroup2->uri, $hrefs));
316     }
317
318     public function testRepeatLink()
319     {
320         $notice = $this->_fakeNotice($this->author1);
321         $repeat = $notice->repeat($this->author2->id, 'test');
322
323         $entry = $repeat->asAtomEntry();
324
325         $element = $this->_entryToElement($entry, true);
326
327         $forward = ActivityUtils::child($element, 'forward', "http://ostatus.org/schema/1.0");
328
329         $this->assertNotNull($forward);
330         $this->assertEquals($notice->uri, $forward->getAttribute('ref'));
331         $this->assertEquals($notice->bestUrl(), $forward->getAttribute('href'));
332     }
333
334     public function testTag()
335     {
336         $tag1 = common_good_rand(4);
337
338         $notice = $this->_fakeNotice($this->author1, '#' . $tag1);
339
340         $entry = $notice->asAtomEntry();
341
342         $element = $this->_entryToElement($entry, true);
343
344         $category = ActivityUtils::child($element, 'category');
345
346         $this->assertNotNull($category);
347         $this->assertEquals($tag1, $category->getAttribute('term'));
348     }
349
350     public function testMultiTag()
351     {
352         $tag1 = common_good_rand(4);
353         $tag2 = common_good_rand(4);
354
355         $notice = $this->_fakeNotice($this->author1, '#' . $tag1 . ' #' . $tag2);
356
357         $entry = $notice->asAtomEntry();
358
359         $element = $this->_entryToElement($entry, true);
360
361         $categories = $element->getElementsByTagName('category');
362
363         $this->assertNotNull($categories);
364         $this->assertEquals(2, $categories->length);
365
366         $terms = array();
367
368         for ($i = 0; $i < $categories->length; $i++) {
369             $cat = $categories->item($i);
370             $terms[] = $cat->getAttribute('term');
371         }
372
373         $this->assertTrue(in_array($tag1, $terms));
374         $this->assertTrue(in_array($tag2, $terms));
375     }
376
377     public function testGeotaggedActivity()
378     {
379         $notice = Notice::saveNew($this->author1->id, common_good_rand(4), 'test', array('uri' => null, 'lat' => 45.5, 'lon' => -73.6));
380
381         $entry = $notice->asAtomEntry();
382
383         $element = $this->_entryToElement($entry, true);
384
385         $this->assertEquals('45.5 -73.6', ActivityUtils::childContent($element, 'point', "http://www.georss.org/georss"));
386     }
387
388     public function testNoticeInfo()
389     {
390         $notice = $this->_fakeNotice();
391
392         $entry = $notice->asAtomEntry();
393
394         $element = $this->_entryToElement($entry, true);
395
396         $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");
397
398         $this->assertEquals($notice->id, $noticeInfo->getAttribute('local_id'));
399         $this->assertEquals($notice->source, $noticeInfo->getAttribute('source'));
400         $this->assertEquals('', $noticeInfo->getAttribute('repeat_of'));
401         $this->assertEquals('', $noticeInfo->getAttribute('repeated'));
402         $this->assertEquals('', $noticeInfo->getAttribute('favorite'));
403         $this->assertEquals('', $noticeInfo->getAttribute('source_link'));
404     }
405
406     public function testNoticeInfoRepeatOf()
407     {
408         $notice = $this->_fakeNotice();
409
410         $repeat = $notice->repeat($this->author2->id, 'test');
411
412         $entry = $repeat->asAtomEntry();
413
414         $element = $this->_entryToElement($entry, true);
415
416         $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");
417
418         $this->assertEquals($notice->id, $noticeInfo->getAttribute('repeat_of'));
419     }
420
421     public function testNoticeInfoRepeated()
422     {
423         $notice = $this->_fakeNotice();
424
425         $repeat = $notice->repeat($this->author2->id, 'test');
426
427         $entry = $notice->asAtomEntry(false, false, false, $this->author2);
428
429         $element = $this->_entryToElement($entry, true);
430
431         $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");
432
433         $this->assertEquals('true', $noticeInfo->getAttribute('repeated'));
434
435         $entry = $notice->asAtomEntry(false, false, false, $this->targetUser1);
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('false', $noticeInfo->getAttribute('repeated'));
442     }
443
444     public function testNoticeInfoFave()
445     {
446         $notice = $this->_fakeNotice();
447
448         $fave = Fave::addNew($this->author2->getProfile(), $notice);
449
450         // Should be set if user has faved
451
452         $entry = $notice->asAtomEntry(false, false, false, $this->author2);
453
454         $element = $this->_entryToElement($entry, true);
455
456         $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");
457
458         $this->assertEquals('true', $noticeInfo->getAttribute('favorite'));
459
460         // Shouldn't be set if user has not faved
461
462         $entry = $notice->asAtomEntry(false, false, false, $this->targetUser1);
463
464         $element = $this->_entryToElement($entry, true);
465
466         $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");
467
468         $this->assertEquals('false', $noticeInfo->getAttribute('favorite'));
469     }
470
471     public function testConversationLink()
472     {
473         $orig = $this->_fakeNotice($this->targetUser1);
474
475         $text = "@" . $this->targetUser1->nickname . " reply text " . common_good_rand(4);
476
477         $reply = Notice::saveNew($this->author1->id, $text, 'test', array('uri' => null, 'reply_to' => $orig->id));
478
479         $conv = Conversation::staticGet('id', $reply->conversation);
480
481         $entry = $reply->asAtomEntry();
482
483         $element = $this->_entryToElement($entry, true);
484
485         $this->assertEquals($conv->uri, ActivityUtils::getLink($element, 'ostatus:conversation'));
486     }
487
488     function __destruct()
489     {
490         if (!is_null($this->author1)) {
491             $this->author1->delete();
492         }
493
494         if (!is_null($this->author2)) {
495             $this->author2->delete();
496         }
497
498         if (!is_null($this->targetUser1)) {
499             $this->targetUser1->delete();
500         }
501
502         if (!is_null($this->targetUser2)) {
503             $this->targetUser2->delete();
504         }
505
506         if (!is_null($this->targetGroup1)) {
507             $this->targetGroup1->delete();
508         }
509
510         if (!is_null($this->targetGroup2)) {
511             $this->targetGroup2->delete();
512         }
513     }
514
515     private function _fakeNotice($user = null, $text = null)
516     {
517         if (empty($user)) {
518             $user = $this->author1;
519         }
520
521         if (empty($text)) {
522             $text = "fake-o text-o " . common_good_rand(32);
523         }
524
525         return Notice::saveNew($user->id, $text, 'test', array('uri' => null));
526     }
527
528     private function _entryToElement($entry, $namespace = false)
529     {
530         $xml = '<?xml version="1.0" encoding="utf-8"?>'."\n\n";
531         $xml .= '<feed';
532         if ($namespace) {
533             $xml .= ' xmlns="http://www.w3.org/2005/Atom"';
534             $xml .= ' xmlns:thr="http://purl.org/syndication/thread/1.0"';
535             $xml .= ' xmlns:georss="http://www.georss.org/georss"';
536             $xml .= ' xmlns:activity="http://activitystrea.ms/spec/1.0/"';
537             $xml .= ' xmlns:media="http://purl.org/syndication/atommedia"';
538             $xml .= ' xmlns:poco="http://portablecontacts.net/spec/1.0"';
539             $xml .= ' xmlns:ostatus="http://ostatus.org/schema/1.0"';
540             $xml .= ' xmlns:statusnet="http://status.net/schema/api/1/"';
541         }
542         $xml .= '>' . "\n" . $entry . "\n" . '</feed>' . "\n";
543         $doc = DOMDocument::loadXML($xml);
544         $feed = $doc->documentElement;
545         $entries = $feed->getElementsByTagName('entry');
546
547         return $entries->item(0);
548     }
549 }