We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

ActivityGenerationTests.php 20.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php

if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
    print "This script must be run from the command line\n";
    exit();
}

// XXX: we should probably have some common source for this stuff

define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
11 12
define('GNUSOCIAL', true);
define('STATUSNET', true);  // compatibility
13 14 15 16 17 18 19 20 21 22 23 24 25 26

require_once INSTALLDIR . '/lib/common.php';

class ActivityGenerationTests extends PHPUnit_Framework_TestCase
{
    var $author1 = null;
    var $author2 = null;

    var $targetUser1 = null;
    var $targetUser2 = null;

    var $targetGroup1 = null;
    var $targetGroup2 = null;

27
    function __construct()
28
    {
29
        parent::__construct();
30

31 32
        $authorNick1 = 'activitygenerationtestsuser' . common_random_hexstr(4);
        $authorNick2 = 'activitygenerationtestsuser' . common_random_hexstr(4);
33

34 35
        $targetNick1 = 'activitygenerationteststarget' . common_random_hexstr(4);
        $targetNick2 = 'activitygenerationteststarget' . common_random_hexstr(4);
36

37 38
        $groupNick1 = 'activitygenerationtestsgroup' . common_random_hexstr(4);
        $groupNick2 = 'activitygenerationtestsgroup' . common_random_hexstr(4);
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

        $this->author1 = User::register(array('nickname' => $authorNick1,
                                              'email' => $authorNick1 . '@example.net',
                                              'email_confirmed' => true));

        $this->author2 = User::register(array('nickname' => $authorNick2,
                                              'email' => $authorNick2 . '@example.net',
                                              'email_confirmed' => true));

        $this->targetUser1 = User::register(array('nickname' => $targetNick1,
                                                  'email' => $targetNick1 . '@example.net',
                                                  'email_confirmed' => true));

        $this->targetUser2 = User::register(array('nickname' => $targetNick2,
                                                  'email' => $targetNick2 . '@example.net',
                                                  'email_confirmed' => true));

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
        $this->targetGroup1 = User_group::register(array('nickname' => $groupNick1,
                                                         'userid' => $this->author1->id,
                                                         'aliases' => array(),
                                                         'local' => true,
                                                         'location' => null,
                                                         'description' => null,
                                                         'fullname' => null,
                                                         'homepage' => null,
                                                         'mainpage' => null));
        $this->targetGroup2 = User_group::register(array('nickname' => $groupNick2,
                                                         'userid' => $this->author1->id,
                                                         'aliases' => array(),
                                                         'local' => true,
                                                         'location' => null,
                                                         'description' => null,
                                                         'fullname' => null,
                                                         'homepage' => null,
                                                         'mainpage' => null));
74 75 76 77 78 79 80 81 82 83
    }

    public function testBasicNoticeActivity()
    {
        $notice = $this->_fakeNotice();

        $entry = $notice->asAtomEntry(true);

        $element = $this->_entryToElement($entry, false);

84
        $this->assertEquals($notice->getUri(), ActivityUtils::childContent($element, 'id'));
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
        $this->assertEquals($notice->content, ActivityUtils::childContent($element, 'title'));
        $this->assertEquals($notice->rendered, ActivityUtils::childContent($element, 'content'));
        $this->assertEquals(strtotime($notice->created), strtotime(ActivityUtils::childContent($element, 'published')));
        $this->assertEquals(strtotime($notice->created), strtotime(ActivityUtils::childContent($element, 'updated')));
        $this->assertEquals(ActivityVerb::POST, ActivityUtils::childContent($element, 'verb', Activity::SPEC));
        $this->assertEquals(ActivityObject::NOTE, ActivityUtils::childContent($element, 'object-type', Activity::SPEC));
    }

    public function testNamespaceFlag()
    {
        $notice = $this->_fakeNotice();

        $entry = $notice->asAtomEntry(true);

        $element = $this->_entryToElement($entry, false);

        $this->assertTrue($element->hasAttribute('xmlns'));
        $this->assertTrue($element->hasAttribute('xmlns:thr'));
        $this->assertTrue($element->hasAttribute('xmlns:georss'));
        $this->assertTrue($element->hasAttribute('xmlns:activity'));
        $this->assertTrue($element->hasAttribute('xmlns:media'));
        $this->assertTrue($element->hasAttribute('xmlns:poco'));
        $this->assertTrue($element->hasAttribute('xmlns:ostatus'));
        $this->assertTrue($element->hasAttribute('xmlns:statusnet'));

        $entry = $notice->asAtomEntry(false);

        $element = $this->_entryToElement($entry, true);

        $this->assertFalse($element->hasAttribute('xmlns'));
        $this->assertFalse($element->hasAttribute('xmlns:thr'));
        $this->assertFalse($element->hasAttribute('xmlns:georss'));
        $this->assertFalse($element->hasAttribute('xmlns:activity'));
        $this->assertFalse($element->hasAttribute('xmlns:media'));
        $this->assertFalse($element->hasAttribute('xmlns:poco'));
        $this->assertFalse($element->hasAttribute('xmlns:ostatus'));
        $this->assertFalse($element->hasAttribute('xmlns:statusnet'));
    }

124
    public function testSourceFlag()
125
    {
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
        $notice = $this->_fakeNotice();

        // Test with no source

        $entry = $notice->asAtomEntry(false, false);

        $element = $this->_entryToElement($entry, true);

        $source = ActivityUtils::child($element, 'source');

        $this->assertNull($source);

        // Test with source

        $entry = $notice->asAtomEntry(false, true);

        $element = $this->_entryToElement($entry, true);

        $source = ActivityUtils::child($element, 'source');

        $this->assertNotNull($source);
147 148
    }

149
    public function testSourceContent()
150
    {
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
        $notice = $this->_fakeNotice();
        // make a time difference!
        sleep(2);
        $notice2 = $this->_fakeNotice();

        $entry = $notice->asAtomEntry(false, true);

        $element = $this->_entryToElement($entry, true);

        $source = ActivityUtils::child($element, 'source');

        $atomUrl = common_local_url('ApiTimelineUser', array('id' => $this->author1->id, 'format' => 'atom'));

        $profile = $this->author1->getProfile();

        $this->assertEquals($atomUrl, ActivityUtils::childContent($source, 'id'));
        $this->assertEquals($atomUrl, ActivityUtils::getLink($source, 'self', 'application/atom+xml'));
        $this->assertEquals($profile->profileurl, ActivityUtils::getPermalink($source));
Evan Prodromou's avatar
Evan Prodromou committed
169
        $this->assertEquals(strtotime($notice2->created), strtotime(ActivityUtils::childContent($source, 'updated')));
170 171 172
        // XXX: do we care here?
        $this->assertFalse(is_null(ActivityUtils::childContent($source, 'title')));
        $this->assertEquals(common_config('license', 'url'), ActivityUtils::getLink($source, 'license'));
173 174
    }

175
    public function testAuthorFlag()
176
    {
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
        $notice = $this->_fakeNotice();

        // Test with no author

        $entry = $notice->asAtomEntry(false, false, false);

        $element = $this->_entryToElement($entry, true);

        $this->assertNull(ActivityUtils::child($element, 'author'));
        $this->assertNull(ActivityUtils::child($element, 'actor', Activity::SPEC));

        // Test with source

        $entry = $notice->asAtomEntry(false, false, true);

        $element = $this->_entryToElement($entry, true);

        $author = ActivityUtils::child($element, 'author');
        $actor  = ActivityUtils::child($element, 'actor', Activity::SPEC);

        $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 204 205 206 207 208 209 210 211 212
        $notice = $this->_fakeNotice();

        // Test with author

        $entry = $notice->asAtomEntry(false, false, true);

        $element = $this->_entryToElement($entry, true);

        $author = ActivityUtils::child($element, 'author');

213 214
        $this->assertEquals($this->author1->getNickname(), ActivityUtils::childContent($author, 'name'));
        $this->assertEquals($this->author1->getUri(), ActivityUtils::childContent($author, 'uri'));
215 216
    }

217 218 219
    /**
     * We no longer create <activity:actor> entries, they have merged to <atom:author>
     */
Evan Prodromou's avatar
Evan Prodromou committed
220 221 222 223 224 225 226 227 228 229 230 231
    public function testActorContent()
    {
        $notice = $this->_fakeNotice();

        // Test with author

        $entry = $notice->asAtomEntry(false, false, true);

        $element = $this->_entryToElement($entry, true);

        $actor = ActivityUtils::child($element, 'actor', Activity::SPEC);

232
        $this->assertEquals($actor, null);
Evan Prodromou's avatar
Evan Prodromou committed
233 234
    }

235 236 237 238
    public function testReplyLink()
    {
        $orig = $this->_fakeNotice($this->targetUser1);

239
        $text = "@" . $this->targetUser1->nickname . " reply text " . common_random_hexstr(4);
240 241 242 243 244 245 246 247 248 249

        $reply = Notice::saveNew($this->author1->id, $text, 'test', array('uri' => null, 'reply_to' => $orig->id));

        $entry = $reply->asAtomEntry();

        $element = $this->_entryToElement($entry, true);

        $irt = ActivityUtils::child($element, 'in-reply-to', 'http://purl.org/syndication/thread/1.0');

        $this->assertNotNull($irt);
250 251
        $this->assertEquals($orig->getUri(), $irt->getAttribute('ref'));
        $this->assertEquals($orig->getUrl(), $irt->getAttribute('href'));
252 253 254 255 256 257
    }

    public function testReplyAttention()
    {
        $orig = $this->_fakeNotice($this->targetUser1);

258
        $text = "@" . $this->targetUser1->nickname . " reply text " . common_random_hexstr(4);
259 260 261 262 263 264 265

        $reply = Notice::saveNew($this->author1->id, $text, 'test', array('uri' => null, 'reply_to' => $orig->id));

        $entry = $reply->asAtomEntry();

        $element = $this->_entryToElement($entry, true);

266
        $this->assertEquals($this->targetUser1->getUri(), ActivityUtils::getLink($element, 'mentioned'));
267 268 269 270 271 272
    }

    public function testMultipleReplyAttention()
    {
        $orig = $this->_fakeNotice($this->targetUser1);

273
        $text = "@" . $this->targetUser1->nickname . " reply text " . common_random_hexstr(4);
274 275 276

        $reply = Notice::saveNew($this->targetUser2->id, $text, 'test', array('uri' => null, 'reply_to' => $orig->id));

277
        $text = "@" . $this->targetUser1->nickname . " @" . $this->targetUser2->nickname . " reply text " . common_random_hexstr(4);
278 279 280 281 282 283 284

        $reply2 = Notice::saveNew($this->author1->id, $text, 'test', array('uri' => null, 'reply_to' => $reply->id));

        $entry = $reply2->asAtomEntry();

        $element = $this->_entryToElement($entry, true);

285
        $links = ActivityUtils::getLinks($element, 'mentioned');
286 287 288 289 290 291 292 293 294

        $this->assertEquals(2, count($links));

        $hrefs = array();

        foreach ($links as $link) {
            $hrefs[] = $link->getAttribute('href');
        }

295 296
        $this->assertTrue(in_array($this->targetUser1->getUri(), $hrefs));
        $this->assertTrue(in_array($this->targetUser2->getUri(), $hrefs));
297 298 299 300 301 302 303 304 305 306 307

        $links = ActivityUtils::getLinks($element, 'mentioned');

        $this->assertEquals(2, count($links));

        $hrefs = array();

        foreach ($links as $link) {
            $hrefs[] = $link->getAttribute('href');
        }

308 309
        $this->assertTrue(in_array($this->targetUser1->getUri(), $hrefs));
        $this->assertTrue(in_array($this->targetUser2->getUri(), $hrefs));
310 311 312 313
    }

    public function testGroupPostAttention()
    {
314
        $text = "!" . $this->targetGroup1->nickname . " reply text " . common_random_hexstr(4);
315 316 317 318 319 320 321

        $notice = Notice::saveNew($this->author1->id, $text, 'test', array('uri' => null));

        $entry = $notice->asAtomEntry();

        $element = $this->_entryToElement($entry, true);

322
        $this->assertEquals($this->targetGroup1->getUri(), ActivityUtils::getLink($element, 'mentioned'));
323 324 325 326
    }

    public function testMultipleGroupPostAttention()
    {
327
        $text = "!" . $this->targetGroup1->nickname . " !" . $this->targetGroup2->nickname . " reply text " . common_random_hexstr(4);
328 329 330 331 332 333 334

        $notice = Notice::saveNew($this->author1->id, $text, 'test', array('uri' => null));

        $entry = $notice->asAtomEntry();

        $element = $this->_entryToElement($entry, true);

335
        $links = ActivityUtils::getLinks($element, 'mentioned');
336 337 338 339 340 341 342 343 344

        $this->assertEquals(2, count($links));

        $hrefs = array();

        foreach ($links as $link) {
            $hrefs[] = $link->getAttribute('href');
        }

345 346
        $this->assertTrue(in_array($this->targetGroup1->getUri(), $hrefs));
        $this->assertTrue(in_array($this->targetGroup2->getUri(), $hrefs));
347 348 349 350 351 352 353 354 355 356 357

        $links = ActivityUtils::getLinks($element, 'mentioned');

        $this->assertEquals(2, count($links));

        $hrefs = array();

        foreach ($links as $link) {
            $hrefs[] = $link->getAttribute('href');
        }

358 359
        $this->assertTrue(in_array($this->targetGroup1->getUri(), $hrefs));
        $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 368 369 370 371 372 373

        $entry = $repeat->asAtomEntry();

        $element = $this->_entryToElement($entry, true);

        $forward = ActivityUtils::child($element, 'forward', "http://ostatus.org/schema/1.0");

        $this->assertNotNull($forward);
374
        $this->assertEquals($notice->getUri(), $forward->getAttribute('ref'));
375
        $this->assertEquals($notice->getUrl(), $forward->getAttribute('href'));
376 377 378 379
    }

    public function testTag()
    {
380
        $tag1 = common_random_hexstr(4);
381 382 383 384 385 386 387 388 389 390 391

        $notice = $this->_fakeNotice($this->author1, '#' . $tag1);

        $entry = $notice->asAtomEntry();

        $element = $this->_entryToElement($entry, true);

        $category = ActivityUtils::child($element, 'category');

        $this->assertNotNull($category);
        $this->assertEquals($tag1, $category->getAttribute('term'));
392 393
    }

394
    public function testMultiTag()
395
    {
396 397
        $tag1 = common_random_hexstr(4);
        $tag2 = common_random_hexstr(4);
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418

        $notice = $this->_fakeNotice($this->author1, '#' . $tag1 . ' #' . $tag2);

        $entry = $notice->asAtomEntry();

        $element = $this->_entryToElement($entry, true);

        $categories = $element->getElementsByTagName('category');

        $this->assertNotNull($categories);
        $this->assertEquals(2, $categories->length);

        $terms = array();

        for ($i = 0; $i < $categories->length; $i++) {
            $cat = $categories->item($i);
            $terms[] = $cat->getAttribute('term');
        }

        $this->assertTrue(in_array($tag1, $terms));
        $this->assertTrue(in_array($tag2, $terms));
419 420 421 422
    }

    public function testGeotaggedActivity()
    {
423
        $notice = Notice::saveNew($this->author1->id, common_random_hexstr(4), 'test', array('uri' => null, 'lat' => 45.5, 'lon' => -73.6));
424 425 426 427 428 429

        $entry = $notice->asAtomEntry();

        $element = $this->_entryToElement($entry, true);

        $this->assertEquals('45.5 -73.6', ActivityUtils::childContent($element, 'point', "http://www.georss.org/georss"));
430 431
    }

432
    public function testNoticeInfo()
433
    {
434 435 436 437 438 439 440 441 442 443 444 445
        $notice = $this->_fakeNotice();

        $entry = $notice->asAtomEntry();

        $element = $this->_entryToElement($entry, true);

        $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");

        $this->assertEquals($notice->id, $noticeInfo->getAttribute('local_id'));
        $this->assertEquals($notice->source, $noticeInfo->getAttribute('source'));
        $this->assertEquals('', $noticeInfo->getAttribute('repeat_of'));
        $this->assertEquals('', $noticeInfo->getAttribute('repeated'));
446
//        $this->assertEquals('', $noticeInfo->getAttribute('favorite'));
447 448 449 450 451 452 453
        $this->assertEquals('', $noticeInfo->getAttribute('source_link'));
    }

    public function testNoticeInfoRepeatOf()
    {
        $notice = $this->_fakeNotice();

454
        $repeat = $notice->repeat($this->author2->getProfile(), 'test');
455 456 457 458 459 460 461 462 463 464 465 466 467 468

        $entry = $repeat->asAtomEntry();

        $element = $this->_entryToElement($entry, true);

        $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");

        $this->assertEquals($notice->id, $noticeInfo->getAttribute('repeat_of'));
    }

    public function testNoticeInfoRepeated()
    {
        $notice = $this->_fakeNotice();

469
        $repeat = $notice->repeat($this->author2->getProfile(), 'test');
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487

        $entry = $notice->asAtomEntry(false, false, false, $this->author2);

        $element = $this->_entryToElement($entry, true);

        $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");

        $this->assertEquals('true', $noticeInfo->getAttribute('repeated'));

        $entry = $notice->asAtomEntry(false, false, false, $this->targetUser1);

        $element = $this->_entryToElement($entry, true);

        $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");

        $this->assertEquals('false', $noticeInfo->getAttribute('repeated'));
    }

488
/*    public function testNoticeInfoFave()
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
    {
        $notice = $this->_fakeNotice();

        $fave = Fave::addNew($this->author2->getProfile(), $notice);

        // Should be set if user has faved

        $entry = $notice->asAtomEntry(false, false, false, $this->author2);

        $element = $this->_entryToElement($entry, true);

        $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");

        $this->assertEquals('true', $noticeInfo->getAttribute('favorite'));

        // Shouldn't be set if user has not faved

        $entry = $notice->asAtomEntry(false, false, false, $this->targetUser1);

        $element = $this->_entryToElement($entry, true);

        $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");

        $this->assertEquals('false', $noticeInfo->getAttribute('favorite'));
513
    }*/
514 515 516 517 518

    public function testConversationLink()
    {
        $orig = $this->_fakeNotice($this->targetUser1);

519
        $text = "@" . $this->targetUser1->nickname . " reply text " . common_random_hexstr(4);
520 521 522

        $reply = Notice::saveNew($this->author1->id, $text, 'test', array('uri' => null, 'reply_to' => $orig->id));

523
        $conv = Conversation::getKV('id', $reply->conversation);
524 525 526 527 528

        $entry = $reply->asAtomEntry();

        $element = $this->_entryToElement($entry, true);

529
        $this->assertEquals($conv->getUri(), ActivityUtils::getLink($element, 'ostatus:conversation'));
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
    }

    function __destruct()
    {
        if (!is_null($this->author1)) {
            $this->author1->delete();
        }

        if (!is_null($this->author2)) {
            $this->author2->delete();
        }

        if (!is_null($this->targetUser1)) {
            $this->targetUser1->delete();
        }

        if (!is_null($this->targetUser2)) {
            $this->targetUser2->delete();
        }

        if (!is_null($this->targetGroup1)) {
            $this->targetGroup1->delete();
        }

        if (!is_null($this->targetGroup2)) {
            $this->targetGroup2->delete();
        }
557 558 559 560 561 562 563 564 565
    }

    private function _fakeNotice($user = null, $text = null)
    {
        if (empty($user)) {
            $user = $this->author1;
        }

        if (empty($text)) {
566
            $text = "fake-o text-o " . common_random_hexstr(32);
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
        }

        return Notice::saveNew($user->id, $text, 'test', array('uri' => null));
    }

    private function _entryToElement($entry, $namespace = false)
    {
        $xml = '<?xml version="1.0" encoding="utf-8"?>'."\n\n";
        $xml .= '<feed';
        if ($namespace) {
            $xml .= ' xmlns="http://www.w3.org/2005/Atom"';
            $xml .= ' xmlns:thr="http://purl.org/syndication/thread/1.0"';
            $xml .= ' xmlns:georss="http://www.georss.org/georss"';
            $xml .= ' xmlns:activity="http://activitystrea.ms/spec/1.0/"';
            $xml .= ' xmlns:media="http://purl.org/syndication/atommedia"';
            $xml .= ' xmlns:poco="http://portablecontacts.net/spec/1.0"';
            $xml .= ' xmlns:ostatus="http://ostatus.org/schema/1.0"';
            $xml .= ' xmlns:statusnet="http://status.net/schema/api/1/"';
        }
        $xml .= '>' . "\n" . $entry . "\n" . '</feed>' . "\n";
        $doc = DOMDocument::loadXML($xml);
        $feed = $doc->documentElement;
        $entries = $feed->getElementsByTagName('entry');

        return $entries->item(0);
    }
}