From 41d81b996fdd8276cc04e750297a12f852a97bf4 Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Mon, 10 May 2010 15:32:02 -0700 Subject: Test cases for MediaFile::getUploadedFileType() with OpenOffice, MS Office, and PDF sample files (as saved from OpenOffice 3.2) Only 3 of 16 cases pass on my dev box with default config. Ouch! --- tests/MediaFileTest.php | 77 +++++++++++++++++++++++++++++++++ tests/sample-uploads/office.pdf | Bin 0 -> 1162 bytes tests/sample-uploads/presentation.odp | Bin 0 -> 9330 bytes tests/sample-uploads/presentation.otp | Bin 0 -> 9359 bytes tests/sample-uploads/presentation.pot | Bin 0 -> 71168 bytes tests/sample-uploads/presentation.potm | Bin 0 -> 5789 bytes tests/sample-uploads/presentation.ppt | Bin 0 -> 71168 bytes tests/sample-uploads/presentation.pptx | Bin 0 -> 5790 bytes tests/sample-uploads/spreadsheet.ods | Bin 0 -> 6560 bytes tests/sample-uploads/spreadsheet.ots | Bin 0 -> 6575 bytes tests/sample-uploads/spreadsheet.xls | Bin 0 -> 6656 bytes tests/sample-uploads/spreadsheet.xlsx | Bin 0 -> 6010 bytes tests/sample-uploads/spreadsheet.xlt | Bin 0 -> 6144 bytes tests/sample-uploads/wordproc.doc | Bin 0 -> 9216 bytes tests/sample-uploads/wordproc.docx | Bin 0 -> 3350 bytes tests/sample-uploads/wordproc.odt | Bin 0 -> 7641 bytes tests/sample-uploads/wordproc.ott | Bin 0 -> 7656 bytes tests/sample-uploads/wordproc.rtf | 16 +++++++ 18 files changed, 93 insertions(+) create mode 100644 tests/MediaFileTest.php create mode 100644 tests/sample-uploads/office.pdf create mode 100644 tests/sample-uploads/presentation.odp create mode 100644 tests/sample-uploads/presentation.otp create mode 100644 tests/sample-uploads/presentation.pot create mode 100644 tests/sample-uploads/presentation.potm create mode 100644 tests/sample-uploads/presentation.ppt create mode 100644 tests/sample-uploads/presentation.pptx create mode 100644 tests/sample-uploads/spreadsheet.ods create mode 100644 tests/sample-uploads/spreadsheet.ots create mode 100644 tests/sample-uploads/spreadsheet.xls create mode 100644 tests/sample-uploads/spreadsheet.xlsx create mode 100644 tests/sample-uploads/spreadsheet.xlt create mode 100644 tests/sample-uploads/wordproc.doc create mode 100644 tests/sample-uploads/wordproc.docx create mode 100644 tests/sample-uploads/wordproc.odt create mode 100644 tests/sample-uploads/wordproc.ott create mode 100644 tests/sample-uploads/wordproc.rtf (limited to 'tests') diff --git a/tests/MediaFileTest.php b/tests/MediaFileTest.php new file mode 100644 index 000000000..6fe995621 --- /dev/null +++ b/tests/MediaFileTest.php @@ -0,0 +1,77 @@ +old_attachments_supported = common_config('attachments', 'supported'); + $GLOBALS['config']['attachments']['supported'] = true; + } + + public function tearDown() + { + $GLOBALS['config']['attachments']['supported'] = $this->old_attachments_supported; + } + + /** + * @dataProvider fileTypeCases + * + */ + public function testFileType($filename, $expectedType) + { + if (!file_exists($filename)) { + throw new Exception("WTF? $filename test file missing"); + } + $this->assertEquals($expectedType, MediaFile::getUploadedFileType($filename)); + } + + static public function fileTypeCases() + { + $base = dirname(__FILE__); + $dir = "$base/sample-uploads"; + return array( + array("$dir/office.pdf", "application/pdf"), + + array("$dir/wordproc.odt", "application/vnd.oasis.opendocument.text"), + array("$dir/wordproc.ott", "application/vnd.oasis.opendocument.text-template"), + array("$dir/wordproc.doc", "application/msword"), + array("$dir/wordproc.docx", + "application/vnd.openxmlformats-officedocument.wordprocessingml.document"), + array("$dir/wordproc.rtf", "text/rtf"), + + array("$dir/spreadsheet.ods", + "application/vnd.oasis.opendocument.spreadsheet"), + array("$dir/spreadsheet.ots", + "application/vnd.oasis.opendocument.spreadsheet-template"), + array("$dir/spreadsheet.xls", "application/vnd.ms-excel"), + array("$dir/spreadsheet.xlt", "application/vnd.ms-excel"), + array("$dir/spreadsheet.xlsx", + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"), + + array("$dir/presentation.odp", + "application/vnd.oasis-opendocument.presentation"), + array("$dir/presentation.otp", + "application/vnd.oasis-opendocument.presentation-template"), + array("$dir/presentation.ppt", + "application/vnd.ms-powerpoint"), + array("$dir/presentation.pot", + "application/vnd.ms-powerpoint"), + array("$dir/presentation.pptx", + "application/vnd.openxmlformats-officedocument.presentationml.presentation"), + ); + } + +} + diff --git a/tests/sample-uploads/office.pdf b/tests/sample-uploads/office.pdf new file mode 100644 index 000000000..670bc2343 Binary files /dev/null and b/tests/sample-uploads/office.pdf differ diff --git a/tests/sample-uploads/presentation.odp b/tests/sample-uploads/presentation.odp new file mode 100644 index 000000000..8dd3a428b Binary files /dev/null and b/tests/sample-uploads/presentation.odp differ diff --git a/tests/sample-uploads/presentation.otp b/tests/sample-uploads/presentation.otp new file mode 100644 index 000000000..1927ee79d Binary files /dev/null and b/tests/sample-uploads/presentation.otp differ diff --git a/tests/sample-uploads/presentation.pot b/tests/sample-uploads/presentation.pot new file mode 100644 index 000000000..f5124ffa2 Binary files /dev/null and b/tests/sample-uploads/presentation.pot differ diff --git a/tests/sample-uploads/presentation.potm b/tests/sample-uploads/presentation.potm new file mode 100644 index 000000000..ade1bcb10 Binary files /dev/null and b/tests/sample-uploads/presentation.potm differ diff --git a/tests/sample-uploads/presentation.ppt b/tests/sample-uploads/presentation.ppt new file mode 100644 index 000000000..f5124ffa2 Binary files /dev/null and b/tests/sample-uploads/presentation.ppt differ diff --git a/tests/sample-uploads/presentation.pptx b/tests/sample-uploads/presentation.pptx new file mode 100644 index 000000000..21ea61a15 Binary files /dev/null and b/tests/sample-uploads/presentation.pptx differ diff --git a/tests/sample-uploads/spreadsheet.ods b/tests/sample-uploads/spreadsheet.ods new file mode 100644 index 000000000..7b43e7507 Binary files /dev/null and b/tests/sample-uploads/spreadsheet.ods differ diff --git a/tests/sample-uploads/spreadsheet.ots b/tests/sample-uploads/spreadsheet.ots new file mode 100644 index 000000000..5f830e6de Binary files /dev/null and b/tests/sample-uploads/spreadsheet.ots differ diff --git a/tests/sample-uploads/spreadsheet.xls b/tests/sample-uploads/spreadsheet.xls new file mode 100644 index 000000000..2d470e687 Binary files /dev/null and b/tests/sample-uploads/spreadsheet.xls differ diff --git a/tests/sample-uploads/spreadsheet.xlsx b/tests/sample-uploads/spreadsheet.xlsx new file mode 100644 index 000000000..b97a551f8 Binary files /dev/null and b/tests/sample-uploads/spreadsheet.xlsx differ diff --git a/tests/sample-uploads/spreadsheet.xlt b/tests/sample-uploads/spreadsheet.xlt new file mode 100644 index 000000000..980423b20 Binary files /dev/null and b/tests/sample-uploads/spreadsheet.xlt differ diff --git a/tests/sample-uploads/wordproc.doc b/tests/sample-uploads/wordproc.doc new file mode 100644 index 000000000..81c5e34c6 Binary files /dev/null and b/tests/sample-uploads/wordproc.doc differ diff --git a/tests/sample-uploads/wordproc.docx b/tests/sample-uploads/wordproc.docx new file mode 100644 index 000000000..04ea3c3ec Binary files /dev/null and b/tests/sample-uploads/wordproc.docx differ diff --git a/tests/sample-uploads/wordproc.odt b/tests/sample-uploads/wordproc.odt new file mode 100644 index 000000000..fa6fe5e9f Binary files /dev/null and b/tests/sample-uploads/wordproc.odt differ diff --git a/tests/sample-uploads/wordproc.ott b/tests/sample-uploads/wordproc.ott new file mode 100644 index 000000000..99ca8c068 Binary files /dev/null and b/tests/sample-uploads/wordproc.ott differ diff --git a/tests/sample-uploads/wordproc.rtf b/tests/sample-uploads/wordproc.rtf new file mode 100644 index 000000000..aad2c4605 --- /dev/null +++ b/tests/sample-uploads/wordproc.rtf @@ -0,0 +1,16 @@ +{\rtf1\ansi\deff0\adeflang1025 +{\fonttbl{\f0\froman\fprq2\fcharset128 Times New Roman;}{\f1\froman\fprq2\fcharset128 Times New Roman;}{\f2\fswiss\fprq2\fcharset128 Arial;}{\f3\fnil\fprq2\fcharset128 DejaVu Sans;}} +{\colortbl;\red0\green0\blue0;\red128\green128\blue128;} +{\stylesheet{\s1\cf0{\*\hyphen2\hyphlead2\hyphtrail2\hyphmax0}\rtlch\af3\afs24\lang1081\ltrch\dbch\af3\langfe2052\hich\f0\fs24\lang1033\loch\f0\fs24\lang1033\snext1 Normal;} +{\s2\sb240\sa120\keepn\cf0{\*\hyphen2\hyphlead2\hyphtrail2\hyphmax0}\rtlch\afs28\lang1081\ltrch\dbch\langfe2052\hich\f2\fs28\lang1033\loch\f2\fs28\lang1033\sbasedon1\snext3 Heading;} +{\s3\sa120\cf0{\*\hyphen2\hyphlead2\hyphtrail2\hyphmax0}\rtlch\af3\afs24\lang1081\ltrch\dbch\af3\langfe2052\hich\f0\fs24\lang1033\loch\f0\fs24\lang1033\sbasedon1\snext3 Body Text;} +{\s4\sa120\cf0{\*\hyphen2\hyphlead2\hyphtrail2\hyphmax0}\rtlch\af3\afs24\lang1081\ltrch\dbch\af3\langfe2052\hich\f0\fs24\lang1033\loch\f0\fs24\lang1033\sbasedon3\snext4 List;} +{\s5\sb120\sa120\cf0{\*\hyphen2\hyphlead2\hyphtrail2\hyphmax0}\rtlch\af3\afs24\lang1081\ai\ltrch\dbch\af3\langfe2052\hich\f0\fs24\lang1033\i\loch\f0\fs24\lang1033\i\sbasedon1\snext5 caption;} +{\s6\cf0{\*\hyphen2\hyphlead2\hyphtrail2\hyphmax0}\rtlch\af3\afs24\lang1081\ltrch\dbch\af3\langfe2052\hich\f0\fs24\lang1033\loch\f0\fs24\lang1033\sbasedon1\snext6 Index;} +} +{\info{\author Brion }{\creatim\yr2010\mo5\dy10\hr15\min2}{\revtim\yr0\mo0\dy0\hr0\min0}{\printim\yr0\mo0\dy0\hr0\min0}{\comment StarWriter}{\vern3200}}\deftab709 +{\*\pgdsctbl +{\pgdsc0\pgdscuse195\pgwsxn12240\pghsxn15840\marglsxn1134\margrsxn1134\margtsxn1134\margbsxn1134\pgdscnxt0 Standard;}} +\paperh15840\paperw12240\margl1134\margr1134\margt1134\margb1134\sectd\sbknone\pgwsxn12240\pghsxn15840\marglsxn1134\margrsxn1134\margtsxn1134\margbsxn1134\ftnbj\ftnstart1\ftnrstcont\ftnnar\aenddoc\aftnrstcont\aftnstart1\aftnnrlc +\pard\plain \ltrpar\s1\cf0{\*\hyphen2\hyphlead2\hyphtrail2\hyphmax0}\rtlch\af3\afs24\lang1081\ltrch\dbch\af3\langfe2052\hich\f0\fs24\lang1033\loch\f0\fs24\lang1033 +\par } \ No newline at end of file -- cgit v1.2.3-54-g00ecf From 9c7b66984c46668f314f93337d28c62854b6d134 Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Mon, 10 May 2010 16:18:29 -0700 Subject: Enhanced upload file type detection. If given an original filename, we'll attempt to detect type from the extension if we were unable to make a definitive match from content. Generic octet-stream, zip, and MS Office type are explicitly singled out for re-checks, which fixes OpenOffice and MS Office documents to come up with the proper types when misdetected. File extensions can also be added to the upload type whitelist; they'll be normalized to types for the actual comparison, so only known extensions will work. --- lib/mediafile.php | 54 +++++++++++++++++++++++++++-- tests/MediaFileTest.php | 73 +++++++++++++++++++++++++--------------- tests/sample-uploads/image.gif | Bin 0 -> 35 bytes tests/sample-uploads/image.jpeg | Bin 0 -> 306 bytes tests/sample-uploads/image.jpg | Bin 0 -> 306 bytes tests/sample-uploads/image.png | Bin 0 -> 159 bytes 6 files changed, 97 insertions(+), 30 deletions(-) create mode 100644 tests/sample-uploads/image.gif create mode 100644 tests/sample-uploads/image.jpeg create mode 100644 tests/sample-uploads/image.jpg create mode 100644 tests/sample-uploads/image.png (limited to 'tests') diff --git a/lib/mediafile.php b/lib/mediafile.php index 10d90d008..85d673d92 100644 --- a/lib/mediafile.php +++ b/lib/mediafile.php @@ -180,7 +180,8 @@ class MediaFile return; } - $mimetype = MediaFile::getUploadedFileType($_FILES[$param]['tmp_name']); + $mimetype = MediaFile::getUploadedFileType($_FILES[$param]['tmp_name'], + $_FILES[$param]['name']); $filename = null; @@ -241,19 +242,41 @@ class MediaFile return new MediaFile($user, $filename, $mimetype); } - static function getUploadedFileType($f) { + /** + * Attempt to identify the content type of a given file. + * + * @param mixed $f file handle resource, or filesystem path as string + * @param string $originalFilename (optional) for extension-based detection + * @return string + * + * @fixme is this an internal or public method? It's called from GetFileAction + * @fixme this seems to tie a front-end error message in, kinda confusing + * @fixme this looks like it could return a PEAR_Error in some cases, if + * type can't be identified and $config['attachments']['supported'] is true + * + * @throws ClientException if type is known, but not supported for local uploads + */ + static function getUploadedFileType($f, $originalFilename=false) { require_once 'MIME/Type.php'; + require_once 'MIME/Type/Extension.php'; + $mte = new MIME_Type_Extension(); $cmd = &PEAR::getStaticProperty('MIME_Type', 'fileCmd'); $cmd = common_config('attachments', 'filecommand'); $filetype = null; + // If we couldn't get a clear type from the file extension, + // we'll go ahead and try checking the content. Content checks + // are unambiguous for most image files, but nearly useless + // for office document formats. + if (is_string($f)) { // assuming a filename $filetype = MIME_Type::autoDetect($f); + } else { // assuming a filehandle @@ -262,7 +285,32 @@ class MediaFile $filetype = MIME_Type::autoDetect($stream['uri']); } - if (common_config('attachments', 'supported') === true || in_array($filetype, common_config('attachments', 'supported'))) { + // The content-based sources for MIME_Type::autoDetect() + // are wildly unreliable for office-type documents. If we've + // gotten an unclear reponse back or just couldn't identify it, + // we'll try detecting a type from its extension... + $unclearTypes = array('application/octet-stream', + 'application/vnd.ms-office', + 'application/zip'); + + if ($originalFilename && (!$filetype || in_array($filetype, $unclearTypes))) { + $type = $mte->getMIMEType($originalFilename); + if (is_string($type)) { + $filetype = $type; + } + } + + $supported = common_config('attachments', 'supported'); + if (is_array($supported)) { + // Normalize extensions to mime types + foreach ($supported as $i => $entry) { + if (strpos($entry, '/') === false) { + common_log(LOG_INFO, "sample.$entry"); + $supported[$i] = $mte->getMIMEType("sample.$entry"); + } + } + } + if ($supported === true || in_array($filetype, $supported)) { return $filetype; } $media = MIME_Type::getMedia($filetype); diff --git a/tests/MediaFileTest.php b/tests/MediaFileTest.php index 6fe995621..a76a4f45e 100644 --- a/tests/MediaFileTest.php +++ b/tests/MediaFileTest.php @@ -34,43 +34,62 @@ class MediaFileTest extends PHPUnit_Framework_TestCase if (!file_exists($filename)) { throw new Exception("WTF? $filename test file missing"); } - $this->assertEquals($expectedType, MediaFile::getUploadedFileType($filename)); + + $type = MediaFile::getUploadedFileType($filename, basename($filename)); + $this->assertEquals($expectedType, $type); + } + + /** + * @dataProvider fileTypeCases + * + */ + public function testUploadedFileType($filename, $expectedType) + { + if (!file_exists($filename)) { + throw new Exception("WTF? $filename test file missing"); + } + $tmp = tmpfile(); + fwrite($tmp, file_get_contents($filename)); + + $type = MediaFile::getUploadedFileType($tmp, basename($filename)); + $this->assertEquals($expectedType, $type); } static public function fileTypeCases() { $base = dirname(__FILE__); $dir = "$base/sample-uploads"; - return array( - array("$dir/office.pdf", "application/pdf"), + $files = array( + "image.png" => "image/png", + "image.gif" => "image/gif", + "image.jpg" => "image/jpeg", + "image.jpeg" => "image/jpeg", + + "office.pdf" => "application/pdf", - array("$dir/wordproc.odt", "application/vnd.oasis.opendocument.text"), - array("$dir/wordproc.ott", "application/vnd.oasis.opendocument.text-template"), - array("$dir/wordproc.doc", "application/msword"), - array("$dir/wordproc.docx", - "application/vnd.openxmlformats-officedocument.wordprocessingml.document"), - array("$dir/wordproc.rtf", "text/rtf"), + "wordproc.odt" => "application/vnd.oasis.opendocument.text", + "wordproc.ott" => "application/vnd.oasis.opendocument.text-template", + "wordproc.doc" => "application/msword", + "wordproc.docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document", + "wordproc.rtf" => "text/rtf", - array("$dir/spreadsheet.ods", - "application/vnd.oasis.opendocument.spreadsheet"), - array("$dir/spreadsheet.ots", - "application/vnd.oasis.opendocument.spreadsheet-template"), - array("$dir/spreadsheet.xls", "application/vnd.ms-excel"), - array("$dir/spreadsheet.xlt", "application/vnd.ms-excel"), - array("$dir/spreadsheet.xlsx", - "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"), + "spreadsheet.ods" => "application/vnd.oasis.opendocument.spreadsheet", + "spreadsheet.ots" => "application/vnd.oasis.opendocument.spreadsheet-template", + "spreadsheet.xls" => "application/vnd.ms-excel", + "spreadsheet.xlt" => "application/vnd.ms-excel", + "spreadsheet.xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", - array("$dir/presentation.odp", - "application/vnd.oasis-opendocument.presentation"), - array("$dir/presentation.otp", - "application/vnd.oasis-opendocument.presentation-template"), - array("$dir/presentation.ppt", - "application/vnd.ms-powerpoint"), - array("$dir/presentation.pot", - "application/vnd.ms-powerpoint"), - array("$dir/presentation.pptx", - "application/vnd.openxmlformats-officedocument.presentationml.presentation"), + "presentation.odp" => "application/vnd.oasis.opendocument.presentation", + "presentation.otp" => "application/vnd.oasis.opendocument.presentation-template", + "presentation.ppt" => "application/vnd.ms-powerpoint", + "presentation.pptx" => "application/vnd.openxmlformats-officedocument.presentationml.presentation", ); + + $dataset = array(); + foreach ($files as $file => $type) { + $dataset[] = array("$dir/$file", $type); + } + return $dataset; } } diff --git a/tests/sample-uploads/image.gif b/tests/sample-uploads/image.gif new file mode 100644 index 000000000..b636f4b8d Binary files /dev/null and b/tests/sample-uploads/image.gif differ diff --git a/tests/sample-uploads/image.jpeg b/tests/sample-uploads/image.jpeg new file mode 100644 index 000000000..21fcb5aef Binary files /dev/null and b/tests/sample-uploads/image.jpeg differ diff --git a/tests/sample-uploads/image.jpg b/tests/sample-uploads/image.jpg new file mode 100644 index 000000000..21fcb5aef Binary files /dev/null and b/tests/sample-uploads/image.jpg differ diff --git a/tests/sample-uploads/image.png b/tests/sample-uploads/image.png new file mode 100644 index 000000000..60cbcfd17 Binary files /dev/null and b/tests/sample-uploads/image.png differ -- cgit v1.2.3-54-g00ecf From 1a6148f0e41708c32fd410cc7530d9fb1efebcae Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Tue, 3 Aug 2010 13:41:44 -0700 Subject: initial unit tests for activity generation --- tests/ActivityGenerationTests.php | 182 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 182 insertions(+) create mode 100644 tests/ActivityGenerationTests.php (limited to 'tests') diff --git a/tests/ActivityGenerationTests.php b/tests/ActivityGenerationTests.php new file mode 100644 index 000000000..a2e3c2480 --- /dev/null +++ b/tests/ActivityGenerationTests.php @@ -0,0 +1,182 @@ +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)); + + } + + public function testBasicNoticeActivity() + { + $notice = $this->_fakeNotice(); + + $entry = $notice->asAtomEntry(true); + + echo $entry; + + $element = $this->_entryToElement($entry, false); + + $this->assertEquals($notice->uri, ActivityUtils::childContent($element, 'id')); + $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')); + } + + public function testReplyActivity() + { + $this->assertTrue(FALSE); + } + + public function testMultipleReplyActivity() + { + $this->assertTrue(FALSE); + } + + public function testGroupPostActivity() + { + $this->assertTrue(FALSE); + } + + public function testMultipleGroupPostActivity() + { + $this->assertTrue(FALSE); + } + + public function testRepeatActivity() + { + $this->assertTrue(FALSE); + } + + public function testTaggedActivity() + { + $this->assertTrue(FALSE); + } + + public function testGeotaggedActivity() + { + $this->assertTrue(FALSE); + } + + public function tearDown() + { + $this->author1->delete(); + $this->author2->delete(); + $this->targetUser1->delete(); + $this->targetUser2->delete(); + } + + private function _fakeNotice($user = null, $text = null) + { + if (empty($user)) { + $user = $this->author1; + } + + if (empty($text)) { + $text = "fake-o text-o " . common_good_rand(32); + } + + return Notice::saveNew($user->id, $text, 'test', array('uri' => null)); + } + + private function _entryToElement($entry, $namespace = false) + { + $xml = ''."\n\n"; + $xml .= '' . "\n"; + $doc = DOMDocument::loadXML($xml); + $feed = $doc->documentElement; + $entries = $feed->getElementsByTagName('entry'); + + return $entries->item(0); + } +} -- cgit v1.2.3-54-g00ecf From 6756a752c4835f4fd7ab46266f008b2d0a594fb1 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Tue, 3 Aug 2010 14:17:36 -0700 Subject: add some more tests for replies and group posts --- tests/ActivityGenerationTests.php | 249 ++++++++++++++++++++++++++++++++++---- 1 file changed, 227 insertions(+), 22 deletions(-) (limited to 'tests') diff --git a/tests/ActivityGenerationTests.php b/tests/ActivityGenerationTests.php index a2e3c2480..9c7f13208 100644 --- a/tests/ActivityGenerationTests.php +++ b/tests/ActivityGenerationTests.php @@ -23,16 +23,18 @@ class ActivityGenerationTests extends PHPUnit_Framework_TestCase var $targetGroup1 = null; var $targetGroup2 = null; - public function setUp() + function __construct() { - $authorNick1 = 'activitygenerationtestsuser' . common_good_rand(16); - $authorNick2 = 'activitygenerationtestsuser' . common_good_rand(16); + parent::__construct(); - $targetNick1 = 'activitygenerationteststarget' . common_good_rand(16); - $targetNick2 = 'activitygenerationteststarget' . common_good_rand(16); + $authorNick1 = 'activitygenerationtestsuser' . common_good_rand(4); + $authorNick2 = 'activitygenerationtestsuser' . common_good_rand(4); - $groupNick1 = 'activitygenerationtestsgroup' . common_good_rand(16); - $groupNick2 = 'activitygenerationtestsgroup' . common_good_rand(16); + $targetNick1 = 'activitygenerationteststarget' . common_good_rand(4); + $targetNick2 = 'activitygenerationteststarget' . common_good_rand(4); + + $groupNick1 = 'activitygenerationtestsgroup' . common_good_rand(4); + $groupNick2 = 'activitygenerationtestsgroup' . common_good_rand(4); $this->author1 = User::register(array('nickname' => $authorNick1, 'email' => $authorNick1 . '@example.net', @@ -50,6 +52,24 @@ class ActivityGenerationTests extends PHPUnit_Framework_TestCase 'email' => $targetNick2 . '@example.net', 'email_confirmed' => true)); + $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)); } public function testBasicNoticeActivity() @@ -58,8 +78,6 @@ class ActivityGenerationTests extends PHPUnit_Framework_TestCase $entry = $notice->asAtomEntry(true); - echo $entry; - $element = $this->_entryToElement($entry, false); $this->assertEquals($notice->uri, ActivityUtils::childContent($element, 'id')); @@ -102,27 +120,190 @@ class ActivityGenerationTests extends PHPUnit_Framework_TestCase $this->assertFalse($element->hasAttribute('xmlns:statusnet')); } - public function testReplyActivity() + public function testSourceFlag() { - $this->assertTrue(FALSE); + $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); } - public function testMultipleReplyActivity() + public function testSourceContent() { - $this->assertTrue(FALSE); + $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)); + $this->assertEquals($notice2->created, strtotime(ActivityUtils::childContent($source, 'updated'))); + // XXX: do we care here? + $this->assertFalse(is_null(ActivityUtils::childContent($source, 'title'))); + $this->assertEquals(common_config('license', 'url'), ActivityUtils::getLink($source, 'license')); } - public function testGroupPostActivity() + public function testAuthorFlag() { - $this->assertTrue(FALSE); + $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)); + $this->assertFalse(is_null($actor)); } - public function testMultipleGroupPostActivity() + public function testCurArgument() { $this->assertTrue(FALSE); } - public function testRepeatActivity() + public function testReplyLink() + { + $orig = $this->_fakeNotice($this->targetUser1); + + $text = "@" . $this->targetUser1->nickname . " reply text " . common_good_rand(4); + + $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); + $this->assertEquals($orig->uri, $irt->getAttribute('ref')); + $this->assertEquals($orig->bestUrl(), $irt->getAttribute('href')); + } + + public function testReplyAttention() + { + $orig = $this->_fakeNotice($this->targetUser1); + + $text = "@" . $this->targetUser1->nickname . " reply text " . common_good_rand(4); + + $reply = Notice::saveNew($this->author1->id, $text, 'test', array('uri' => null, 'reply_to' => $orig->id)); + + $entry = $reply->asAtomEntry(); + + $element = $this->_entryToElement($entry, true); + + $this->assertEquals($this->targetUser1->uri, ActivityUtils::getLink($element, 'ostatus:attention')); + } + + public function testMultipleReplyAttention() + { + $orig = $this->_fakeNotice($this->targetUser1); + + $text = "@" . $this->targetUser1->nickname . " reply text " . common_good_rand(4); + + $reply = Notice::saveNew($this->targetUser2->id, $text, 'test', array('uri' => null, 'reply_to' => $orig->id)); + + $text = "@" . $this->targetUser1->nickname . " @" . $this->targetUser2->nickname . " reply text " . common_good_rand(4); + + $reply2 = Notice::saveNew($this->author1->id, $text, 'test', array('uri' => null, 'reply_to' => $reply->id)); + + $entry = $reply2->asAtomEntry(); + + $element = $this->_entryToElement($entry, true); + + $links = ActivityUtils::getLinks($element, 'ostatus:attention'); + + $this->assertEquals(2, count($links)); + + $hrefs = array(); + + foreach ($links as $link) { + $hrefs[] = $link->getAttribute('href'); + } + + $this->assertTrue(in_array($this->targetUser1->uri, $hrefs)); + $this->assertTrue(in_array($this->targetUser2->uri, $hrefs)); + } + + public function testGroupPostAttention() + { + $text = "!" . $this->targetGroup1->nickname . " reply text " . common_good_rand(4); + + $notice = Notice::saveNew($this->author1->id, $text, 'test', array('uri' => null)); + + $entry = $notice->asAtomEntry(); + + $element = $this->_entryToElement($entry, true); + + $this->assertEquals($this->targetGroup1->uri, ActivityUtils::getLink($element, 'ostatus:attention')); + } + + public function testMultipleGroupPostAttention() + { + $text = "!" . $this->targetGroup1->nickname . " !" . $this->targetGroup2->nickname . " reply text " . common_good_rand(4); + + $notice = Notice::saveNew($this->author1->id, $text, 'test', array('uri' => null)); + + $entry = $notice->asAtomEntry(); + + $element = $this->_entryToElement($entry, true); + + $links = ActivityUtils::getLinks($element, 'ostatus:attention'); + + $this->assertEquals(2, count($links)); + + $hrefs = array(); + + foreach ($links as $link) { + $hrefs[] = $link->getAttribute('href'); + } + + $this->assertTrue(in_array($this->targetGroup1->uri, $hrefs)); + $this->assertTrue(in_array($this->targetGroup2->uri, $hrefs)); + } + + public function testRepeatLink() { $this->assertTrue(FALSE); } @@ -137,12 +318,36 @@ class ActivityGenerationTests extends PHPUnit_Framework_TestCase $this->assertTrue(FALSE); } - public function tearDown() + public function testNoticeInfo() { - $this->author1->delete(); - $this->author2->delete(); - $this->targetUser1->delete(); - $this->targetUser2->delete(); + $this->assertTrue(FALSE); + } + + 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(); + } } private function _fakeNotice($user = null, $text = null) -- cgit v1.2.3-54-g00ecf From 8d19162122bef8c0661473fc444cceeab7b5ac5a Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Tue, 3 Aug 2010 15:26:19 -0700 Subject: more tests for activity generation --- tests/ActivityGenerationTests.php | 176 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 169 insertions(+), 7 deletions(-) (limited to 'tests') diff --git a/tests/ActivityGenerationTests.php b/tests/ActivityGenerationTests.php index 9c7f13208..cc82151b9 100644 --- a/tests/ActivityGenerationTests.php +++ b/tests/ActivityGenerationTests.php @@ -197,9 +197,21 @@ class ActivityGenerationTests extends PHPUnit_Framework_TestCase $this->assertFalse(is_null($actor)); } - public function testCurArgument() + public function testAuthorContent() { - $this->assertTrue(FALSE); + $notice = $this->_fakeNotice(); + + // Test with author + + $entry = $notice->asAtomEntry(false, false, true); + + $element = $this->_entryToElement($entry, true); + + $author = ActivityUtils::child($element, 'author'); + $actor = ActivityUtils::child($element, 'actor', Activity::SPEC); + + $this->assertEquals($this->author1->nickname, ActivityUtils::childContent($author, 'name')); + $this->assertEquals($this->author1->uri, ActivityUtils::childContent($author, 'uri')); } public function testReplyLink() @@ -305,22 +317,172 @@ class ActivityGenerationTests extends PHPUnit_Framework_TestCase public function testRepeatLink() { - $this->assertTrue(FALSE); + $notice = $this->_fakeNotice($this->author1); + $repeat = $notice->repeat($this->author2->id, 'test'); + + $entry = $repeat->asAtomEntry(); + + $element = $this->_entryToElement($entry, true); + + $forward = ActivityUtils::child($element, 'forward', "http://ostatus.org/schema/1.0"); + + $this->assertNotNull($forward); + $this->assertEquals($notice->uri, $forward->getAttribute('ref')); + $this->assertEquals($notice->bestUrl(), $forward->getAttribute('href')); + } + + public function testTag() + { + $tag1 = common_good_rand(4); + + $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')); } - public function testTaggedActivity() + public function testMultiTag() { - $this->assertTrue(FALSE); + $tag1 = common_good_rand(4); + $tag2 = common_good_rand(4); + + $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)); } public function testGeotaggedActivity() { - $this->assertTrue(FALSE); + $notice = Notice::saveNew($this->author1->id, common_good_rand(4), 'test', array('uri' => null, 'lat' => 45.5, 'lon' => -73.6)); + + $entry = $notice->asAtomEntry(); + + $element = $this->_entryToElement($entry, true); + + $this->assertEquals('45.5 -73.6', ActivityUtils::childContent($element, 'point', "http://www.georss.org/georss")); } public function testNoticeInfo() { - $this->assertTrue(FALSE); + $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')); + $this->assertEquals('', $noticeInfo->getAttribute('favorite')); + $this->assertEquals('', $noticeInfo->getAttribute('source_link')); + } + + public function testNoticeInfoRepeatOf() + { + $notice = $this->_fakeNotice(); + + $repeat = $notice->repeat($this->author2->id, 'test'); + + $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(); + + $repeat = $notice->repeat($this->author2->id, 'test'); + + $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')); + } + + public function testNoticeInfoFave() + { + $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')); + } + + public function testConversationLink() + { + $orig = $this->_fakeNotice($this->targetUser1); + + $text = "@" . $this->targetUser1->nickname . " reply text " . common_good_rand(4); + + $reply = Notice::saveNew($this->author1->id, $text, 'test', array('uri' => null, 'reply_to' => $orig->id)); + + $conv = Conversation::staticGet('id', $reply->conversation); + + $entry = $reply->asAtomEntry(); + + $element = $this->_entryToElement($entry, true); + + $this->assertEquals($conv->uri, ActivityUtils::getLink($element, 'ostatus:conversation')); } function __destruct() -- cgit v1.2.3-54-g00ecf From 744233c6dc385a5870652ab70e7141e75aaff783 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Tue, 3 Aug 2010 15:49:49 -0700 Subject: add actor info to tests --- tests/ActivityGenerationTests.php | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/ActivityGenerationTests.php b/tests/ActivityGenerationTests.php index cc82151b9..52077ee57 100644 --- a/tests/ActivityGenerationTests.php +++ b/tests/ActivityGenerationTests.php @@ -165,7 +165,7 @@ class ActivityGenerationTests extends PHPUnit_Framework_TestCase $this->assertEquals($atomUrl, ActivityUtils::childContent($source, 'id')); $this->assertEquals($atomUrl, ActivityUtils::getLink($source, 'self', 'application/atom+xml')); $this->assertEquals($profile->profileurl, ActivityUtils::getPermalink($source)); - $this->assertEquals($notice2->created, strtotime(ActivityUtils::childContent($source, 'updated'))); + $this->assertEquals(strtotime($notice2->created), strtotime(ActivityUtils::childContent($source, 'updated'))); // XXX: do we care here? $this->assertFalse(is_null(ActivityUtils::childContent($source, 'title'))); $this->assertEquals(common_config('license', 'url'), ActivityUtils::getLink($source, 'license')); @@ -208,12 +208,27 @@ class ActivityGenerationTests extends PHPUnit_Framework_TestCase $element = $this->_entryToElement($entry, true); $author = ActivityUtils::child($element, 'author'); - $actor = ActivityUtils::child($element, 'actor', Activity::SPEC); $this->assertEquals($this->author1->nickname, ActivityUtils::childContent($author, 'name')); $this->assertEquals($this->author1->uri, ActivityUtils::childContent($author, 'uri')); } + 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); + + $this->assertEquals($this->author1->uri, ActivityUtils::childContent($actor, 'id')); + $this->assertEquals($this->author1->nickname, ActivityUtils::childContent($actor, 'title')); + } + public function testReplyLink() { $orig = $this->_fakeNotice($this->targetUser1); -- cgit v1.2.3-54-g00ecf From b17fc0ca5b90d2cdc957ebc4870fbd2791b9e1b9 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Tue, 3 Aug 2010 16:04:19 -0700 Subject: update tests to include 'mentioned' links --- tests/ActivityGenerationTests.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'tests') diff --git a/tests/ActivityGenerationTests.php b/tests/ActivityGenerationTests.php index 52077ee57..b9e74a570 100644 --- a/tests/ActivityGenerationTests.php +++ b/tests/ActivityGenerationTests.php @@ -261,6 +261,7 @@ class ActivityGenerationTests extends PHPUnit_Framework_TestCase $element = $this->_entryToElement($entry, true); $this->assertEquals($this->targetUser1->uri, ActivityUtils::getLink($element, 'ostatus:attention')); + $this->assertEquals($this->targetUser1->uri, ActivityUtils::getLink($element, 'mentioned')); } public function testMultipleReplyAttention() @@ -291,6 +292,19 @@ class ActivityGenerationTests extends PHPUnit_Framework_TestCase $this->assertTrue(in_array($this->targetUser1->uri, $hrefs)); $this->assertTrue(in_array($this->targetUser2->uri, $hrefs)); + + $links = ActivityUtils::getLinks($element, 'mentioned'); + + $this->assertEquals(2, count($links)); + + $hrefs = array(); + + foreach ($links as $link) { + $hrefs[] = $link->getAttribute('href'); + } + + $this->assertTrue(in_array($this->targetUser1->uri, $hrefs)); + $this->assertTrue(in_array($this->targetUser2->uri, $hrefs)); } public function testGroupPostAttention() @@ -304,6 +318,7 @@ class ActivityGenerationTests extends PHPUnit_Framework_TestCase $element = $this->_entryToElement($entry, true); $this->assertEquals($this->targetGroup1->uri, ActivityUtils::getLink($element, 'ostatus:attention')); + $this->assertEquals($this->targetGroup1->uri, ActivityUtils::getLink($element, 'mentioned')); } public function testMultipleGroupPostAttention() @@ -328,6 +343,19 @@ class ActivityGenerationTests extends PHPUnit_Framework_TestCase $this->assertTrue(in_array($this->targetGroup1->uri, $hrefs)); $this->assertTrue(in_array($this->targetGroup2->uri, $hrefs)); + + $links = ActivityUtils::getLinks($element, 'mentioned'); + + $this->assertEquals(2, count($links)); + + $hrefs = array(); + + foreach ($links as $link) { + $hrefs[] = $link->getAttribute('href'); + } + + $this->assertTrue(in_array($this->targetGroup1->uri, $hrefs)); + $this->assertTrue(in_array($this->targetGroup2->uri, $hrefs)); } public function testRepeatLink() -- cgit v1.2.3-54-g00ecf