diff options
Diffstat (limited to 'tests/unit/includes')
-rw-r--r-- | tests/unit/includes/AccountTest.php | 21 | ||||
-rw-r--r-- | tests/unit/includes/BBCodeTest.php | 31 | ||||
-rw-r--r-- | tests/unit/includes/PhotodriverTest.php | 58 |
3 files changed, 102 insertions, 8 deletions
diff --git a/tests/unit/includes/AccountTest.php b/tests/unit/includes/AccountTest.php index 3978f9d04..66c761ef5 100644 --- a/tests/unit/includes/AccountTest.php +++ b/tests/unit/includes/AccountTest.php @@ -1,9 +1,28 @@ <?php + +use Zotlabs\Tests\Unit\UnitTestCase; + /** * Tests for account handling helper functions. */ +class AccountTest extends UnitTestCase { + + /** + * Test the `get_account_id()` function. + */ + public function test_get_account_id() { + App::set_account(null); + unset($_SESSION['account_id']); + + $this->assertEquals(false, get_account_id(), 'get_account_id() should return false if not authenticated'); + + App::set_account(['account_id' => 36]); + $this->assertEquals(36, get_account_id(), 'get_account_id() should return account from global App object'); + + $_SESSION['account_id'] = 42; + $this->assertEquals(42, get_account_id(), 'get_account_id() should return the account from the session'); + } -class AccountTest extends Zotlabs\Tests\Unit\UnitTestCase { public function test_get_account_by_id_returns_existing_account() { $account = get_account_by_id(42); $this->assertNotFalse($account); diff --git a/tests/unit/includes/BBCodeTest.php b/tests/unit/includes/BBCodeTest.php index 136fc6e0e..982ef4eb9 100644 --- a/tests/unit/includes/BBCodeTest.php +++ b/tests/unit/includes/BBCodeTest.php @@ -23,6 +23,7 @@ namespace Zotlabs\Tests\Unit\includes; +use App; use Zotlabs\Tests\Unit\UnitTestCase; class BBCodeTest extends UnitTestCase { @@ -42,7 +43,7 @@ class BBCodeTest extends UnitTestCase { */ public function test_bbcode_observer(string $src, bool $logged_in, string $lang, string $expected): void { if ($logged_in) { - \App::$observer = [ + App::set_observer([ 'xchan_addr' => '', 'xchan_name' => '', 'xchan_connurl' => '', @@ -50,9 +51,9 @@ class BBCodeTest extends UnitTestCase { // port required in xchan url due to bug in get_rpost_path 'xchan_url' => 'https://example.com:666', - ]; + ]); } else { - \App::$observer = null; + App::set_observer(null); } \App::$language = $lang; @@ -141,20 +142,36 @@ class BBCodeTest extends UnitTestCase { ], 'naked url is converted to link' => [ 'example url: https://example.com', - 'example url: <a href="https://example.com" target="_blank" rel="nofollow noopener">https://example.com</a>' + 'example url: <a href="https://example.com" target="_blank" rel="nofollow noopener">https://example.com</a>' ], 'naked url followed by newline' => [ "https://www.example.com\nhave a great day.", - '<a href="https://www.example.com" target="_blank" rel="nofollow noopener">https://www.example.com</a><br />have a great day.', + '<a href="https://www.example.com" target="_blank" rel="nofollow noopener">https://www.example.com</a><br />have a great day.', ], 'inline naked url' => [ "This is a link https://example.com/some/path more info.", - 'This is a link <a href="https://example.com/some/path" target="_blank" rel="nofollow noopener">https://example.com/some/path</a> more info.', + 'This is a link <a href="https://example.com/some/path" target="_blank" rel="nofollow noopener">https://example.com/some/path</a> more info.', ], 'naked url within code block is not converted to link' => [ "[code]\nhttp://example.com\n[/code]", "<pre><code>http://example.com</code></pre>" ], + 'geo uri is converted to link' => [ + 'example url: [url]geo:37.786971,-122.399677;u=35[/url]', + 'example url: <a href="geo:37.786971,-122.399677;u=35" target="_blank" rel="nofollow noopener">geo:37.786971,-122.399677;u=35</a>' + ], + 'geo uri with label is converted to link' => [ + 'example url: [url=geo:37.786971,-122.399677;u=35(Wikimedia+Foundation)]Wikimedia Foundation[/url]', + 'example url: <a href="geo:37.786971,-122.399677;u=35(Wikimedia+Foundation)" target="_blank" rel="nofollow noopener">Wikimedia Foundation</a>' + ], + 'naked geo uri is converted to link' => [ + 'example url: geo:37.786971,-122.399677;u=35', + 'example url: <a href="geo:37.786971,-122.399677;u=35" target="_blank" rel="nofollow noopener">geo:37.786971,-122.399677;u=35</a>' + ], + 'naked geo uri with label is converted to link' => [ + 'example url: geo:37.78918,-122.40335(Wikimedia+Foundation)', + 'example url: <a href="geo:37.78918,-122.40335(Wikimedia+Foundation)" target="_blank" rel="nofollow noopener">📍Wikimedia Foundation</a>' + ], ]; } @@ -205,7 +222,7 @@ class BBCodeTest extends UnitTestCase { '[rpost=a title]This is the body[/rpost]', true, 'en', - '<a href="https://example.com:666/rpost?f=&title=a+title&body=This+is+the+body" target="_blank" rel="nofollow noopener">https://example.com:666/rpost?f=&title=a+title&body=This+is+the+body</a>', + '<a href="https://example.com:666/rpost?f=&title=a+title&body=This+is+the+body" target="_blank" rel="nofollow noopener">https://example.com:666/rpost?f=&title=a+title&body=This+is+the+body</a>', ], 'unauthenticated observer rpost' => [ '[rpost=a title]This is the body[/rpost]', diff --git a/tests/unit/includes/PhotodriverTest.php b/tests/unit/includes/PhotodriverTest.php index 34dc058b7..db9883589 100644 --- a/tests/unit/includes/PhotodriverTest.php +++ b/tests/unit/includes/PhotodriverTest.php @@ -20,4 +20,62 @@ class PhotodriverTest extends UnitTestCase { $photo = \photo_factory(file_get_contents('images/hz-16.png'), 'image/png'); $this->assertInstanceOf('Zotlabs\Photo\PhotoGd', $photo); } + + // Helper to create a temporary image file + private function createTempImage($type = 'jpeg'): string + { + $tmp = tempnam(sys_get_temp_dir(), 'img'); + switch ($type) { + case 'png': + $im = imagecreatetruecolor(10, 10); + imagepng($im, $tmp); + imagedestroy($im); + break; + case 'jpeg': + default: + $im = imagecreatetruecolor(10, 10); + imagejpeg($im, $tmp); + imagedestroy($im); + break; + } + return $tmp; + } + + public function testGuessImageTypeFromRawData() + { + $filename = 'irrelevant'; + $data = [ + 'body' => file_get_contents($this->createTempImage('jpeg')) + ]; + $result = guess_image_type($filename, $data); + $this->assertEquals('image/jpeg', $result); + } + + public function testGuessImageTypeFromLocalFile() + { + $file = $this->createTempImage('png'); + $result = guess_image_type($file); + $this->assertEquals('image/png', $result); + unlink($file); + } + + public function testGuessImageTypeFromHeaders() + { + $filename = 'irrelevant'; + $data = [ + 'header' => "Content-Type: image/jpeg\nOther: value" + ]; + $result = guess_image_type($filename, $data); + $this->assertEquals('image/jpeg', $result); + } + + public function testGuessImageTypeUnknownTypeReturnsNull() + { + $filename = 'not_an_image.txt'; + $data = [ + 'body' => 'not an image' + ]; + $result = guess_image_type($filename, $data); + $this->assertNull($result); + } } |