Browse Source

Add unittests & check filetype in setLocalAvatar()

TODO: Fix OC_Image->mimetype(), it always returns "image/png"
kondou 11 years ago
parent
commit
2bfe662235
4 changed files with 73 additions and 8 deletions
  1. 8 2
      lib/avatar.php
  2. 4 6
      settings/ajax/newavatar.php
  3. BIN
      tests/data/testavatar.png
  4. 61 0
      tests/lib/avatar.php

+ 8 - 2
lib/avatar.php

@@ -43,11 +43,11 @@ class OC_Avatar {
 	 * @brief sets the users local avatar
 	 * @param $user string user to set the avatar for
 	 * @param $img mixed imagedata to set a new avatar, or false to delete the current avatar
-	 * @param $type string fileextension
+	 * @throws Exception if the provided file is not a jpg or png image
 	 * @throws Exception if the provided image is not valid, or not a square
 	 * @return true on success
 	*/
-	public static function setLocalAvatar ($user, $img, $type) {
+	public static function setLocalAvatar ($user, $img) {
 		$view = new \OC\Files\View('/'.$user);
 
 		if ($img === false) {
@@ -56,6 +56,12 @@ class OC_Avatar {
 			return true;
 		} else {
 			$img = new OC_Image($img);
+			// FIXME this always says "image/png"
+			$type = substr($img->mimeType(), -3);
+			if ($type === 'peg') { $type = 'jpg'; }
+			if ($type !== 'jpg' && $type !== 'png') {
+				throw new Exception();
+			}
 
 			if (!( $img->valid() && ($img->height() === $img->width()) )) {
 				throw new Exception();

+ 4 - 6
settings/ajax/newavatar.php

@@ -7,18 +7,16 @@ $user = OC_User::getUser();
 if(isset($_POST['path'])) {
 	$path = $_POST['path'];
 	if ($path === "false") { // delete avatar
-		\OC_Avatar::setLocalAvatar($user, false, false);
+		\OC_Avatar::setLocalAvatar($user, false);
 	} else { // select an image from own files
 		$view = new \OC\Files\View('/'.$user.'/files');
 		$img = $view->file_get_contents($path);
 
 		$type = substr($path, -3);
-		if ($type === 'peg') { $type = 'jpg'; }
-
-		if ($type === 'jpg' or $type === 'png') {
-			\OC_Avatar::setLocalAvatar($user, $img, $type);
+		try {
+			\OC_Avatar::setLocalAvatar($user, $img);
 			OC_JSON::success();
-		} else {
+		} catch (Exception $e) {
 			OC_JSON::error();
 		}
 	}

BIN
tests/data/testavatar.png


+ 61 - 0
tests/lib/avatar.php

@@ -0,0 +1,61 @@
+<?php
+/**
+ * Copyright (c) 2013 Christopher Schäpers <christopher@schaepers.it>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+class Test_Avatar extends PHPUnit_Framework_TestCase {
+
+	public function testModes() {
+		$this->assertEquals('local', \OC_Avatar::getMode());
+
+		\OC_Config::setValue('avatar', 'local');
+		$this->assertEquals('local', \OC_Avatar::getMode());
+		
+		\OC_Config::setValue('avatar', 'gravatar');
+		$this->assertEquals('gravatar', \OC_Avatar::getMode());
+
+		\OC_Config::setValue('avatar', 'none');
+		$this->assertEquals('none', \OC_Avatar::getMode());
+	}
+
+	public function testDisabledAvatar() {
+		\OC_Config::setValue('avatar', 'none');
+		$this->assertFalse(\OC_Avatar::get(\OC_User::getUser()));
+		$this->assertFalse(\OC_Avatar::get(\OC_User::getUser(), 32));
+	}
+
+	public function testLocalAvatar() {
+		\OC_Config::setValue('avatar', 'local');
+		$this->assertEquals(\OC_Avatar::get(\OC_User::getUser()), \OC_Avatar::wrapIntoImg(\OC_Avatar::getDefaultAvatar(), 'png'));
+
+		$expected = new OC_Image(\OC::$SERVERROOT.'/tests/data/testavatar.png');
+		\OC_Avatar::setLocalAvatar(\OC_User::getUser(), $expected->data());
+		$expected->resize(32);
+		$this->assertEquals($expected, \OC_Avatar::get(\OC_User::getUser()));
+
+		\OC_Avatar::setLocalAvatar(\OC_User::getUser(), false);
+		$this->assertEquals(\OC_Avatar::get(\OC_User::getUser()), \OC_Avatar::wrapIntoImg(\OC_Avatar::getDefaultAvatar(), 'png'));
+	}
+
+	public function testGravatar() {
+		\OC_Preferences::setValue(\OC_User::getUser(), 'settings', 'email', 'someone@example.com');
+		\OC_Config::setValue('avatar', 'gravatar');
+		$expected = "http://www.gravatar.com/avatar/".md5("someone@example.com")."?s=";
+		$this->assertEquals($expected."64", \OC_Avatar::get(\OC_User::getUser()));
+		$this->assertEquals($expected."32", \OC_Avatar::get(\OC_User::getUser(), 32));
+	}
+
+	public function testDefaultAvatar() {
+		$img = new \OC_Image(OC::$SERVERROOT.'/core/img/defaultavatar.png');
+		$img->resize(128);
+		$this->assertEquals((string)$img, \OC_Avatar::getDefaultAvatar(128));
+	}
+
+	public function testWrapIntoImg() {
+		$expected = "data:image/test;base64,DUMMY==123==";
+		$this->assertEquals($expected, \OC_Avatar::wrapIntoImg("DUMMY==123==", "test"));
+	}
+}