diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/core/avatar/avatarcontrollertest.php | 62 | ||||
-rw-r--r-- | tests/data/db_structure.xml | 55 | ||||
-rw-r--r-- | tests/lib/db.php | 85 | ||||
-rw-r--r-- | tests/lib/files/pathverificationtest.php | 10 | ||||
-rw-r--r-- | tests/lib/httphelper.php | 48 | ||||
-rw-r--r-- | tests/lib/image.php | 5 | ||||
-rw-r--r-- | tests/lib/testcase.php | 37 |
7 files changed, 211 insertions, 91 deletions
diff --git a/tests/core/avatar/avatarcontrollertest.php b/tests/core/avatar/avatarcontrollertest.php index f43cd7fedd1..a5456eb6789 100644 --- a/tests/core/avatar/avatarcontrollertest.php +++ b/tests/core/avatar/avatarcontrollertest.php @@ -74,7 +74,6 @@ class AvatarControllerTest extends \Test\TestCase { $this->container['Request'] = $this->getMockBuilder('OCP\IRequest') ->disableOriginalConstructor()->getMock(); - $this->avatarMock = $this->getMockBuilder('OCP\IAvatar') ->disableOriginalConstructor()->getMock(); $this->userMock = $this->getMockBuilder('OCP\IUser') @@ -82,11 +81,11 @@ class AvatarControllerTest extends \Test\TestCase { $this->avatarController = $this->container['AvatarController']; - // Store current User + // Store current User $this->oldUser = \OC_User::getUser(); // Create a dummy user - $this->user = \OC::$server->getSecureRandom()->getLowStrengthGenerator()->generate(12, ISecureRandom::CHAR_LOWER); + $this->user = $this->getUniqueID('user'); OC::$server->getUserManager()->createUser($this->user, $this->user); \OC_Util::tearDownFS(); @@ -102,7 +101,8 @@ class AvatarControllerTest extends \Test\TestCase { // Configure userMock $this->userMock->method('getDisplayName')->willReturn($this->user); $this->userMock->method('getUID')->willReturn($this->user); - $this->container['UserManager']->method('get')->willReturn($this->userMock); + $this->container['UserManager']->method('get') + ->willReturnMap([[$this->user, $this->userMock]]); $this->container['UserSession']->method('getUser')->willReturn($this->userMock); } @@ -128,13 +128,14 @@ class AvatarControllerTest extends \Test\TestCase { $this->container['AvatarManager']->method('getAvatar')->willReturn($this->avatarMock); $response = $this->avatarController->getAvatar($this->user, 32); - //Comment out unitl JS is fixed - //$this->assertEquals($response->getStatus(), Http::STATUS_NOT_FOUND); - $this->assertEquals($response->getData()['data']['displayname'], $this->user); + //Comment out until JS is fixed + //$this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus()); + $this->assertEquals(Http::STATUS_OK, $response->getStatus()); + $this->assertEquals($this->user, $response->getData()['data']['displayname']); } /** - * Fetch the users avatar + * Fetch the user's avatar */ public function testGetAvatar() { $image = new Image(OC::$SERVERROOT.'/tests/data/testimage.jpg'); @@ -143,7 +144,7 @@ class AvatarControllerTest extends \Test\TestCase { $response = $this->avatarController->getAvatar($this->user, 32); - $this->assertEquals($response->getStatus(), Http::STATUS_OK); + $this->assertEquals(Http::STATUS_OK, $response->getStatus()); $image2 = new Image($response->getData()); $this->assertEquals($image->mimeType(), $image2->mimeType()); @@ -151,6 +152,21 @@ class AvatarControllerTest extends \Test\TestCase { } /** + * Fetch the avatar of a non-existing user + */ + public function testGetAvatarNoUser() { + $this->avatarMock->method('get')->willReturn(null); + $this->container['AvatarManager']->method('getAvatar')->willReturn($this->avatarMock); + + $response = $this->avatarController->getAvatar($this->user . 'doesnotexist', 32); + + //Comment out until JS is fixed + //$this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus()); + $this->assertEquals(Http::STATUS_OK, $response->getStatus()); + $this->assertEquals('', $response->getData()['data']['displayname']); + } + + /** * Make sure we get the correct size */ public function testGetAvatarSize() { @@ -196,7 +212,7 @@ class AvatarControllerTest extends \Test\TestCase { $this->container['AvatarManager']->method('getAvatar')->willReturn($this->avatarMock); $response = $this->avatarController->deleteAvatar(); - $this->assertEquals($response->getStatus(), Http::STATUS_OK); + $this->assertEquals(Http::STATUS_OK, $response->getStatus()); } /** @@ -207,7 +223,7 @@ class AvatarControllerTest extends \Test\TestCase { $this->container['AvatarManager']->method('getAvatar')->willReturn($this->avatarMock); $response = $this->avatarController->deleteAvatar(); - $this->assertEquals($response->getStatus(), Http::STATUS_BAD_REQUEST); + $this->assertEquals(Http::STATUS_BAD_REQUEST, $response->getStatus()); } /** @@ -215,7 +231,7 @@ class AvatarControllerTest extends \Test\TestCase { */ public function testTmpAvatarNoTmp() { $response = $this->avatarController->getTmpAvatar(); - $this->assertEquals($response->getStatus(), Http::STATUS_NOT_FOUND); + $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus()); } /** @@ -225,7 +241,7 @@ class AvatarControllerTest extends \Test\TestCase { $this->container['Cache']->method('get')->willReturn(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg')); $response = $this->avatarController->getTmpAvatar(); - $this->assertEquals($response->getStatus(), Http::STATUS_OK); + $this->assertEquals(Http::STATUS_OK, $response->getStatus()); } @@ -235,7 +251,7 @@ class AvatarControllerTest extends \Test\TestCase { public function testPostAvatarNoPathOrImage() { $response = $this->avatarController->postAvatar(null); - $this->assertEquals($response->getStatus(), Http::STATUS_BAD_REQUEST); + $this->assertEquals(Http::STATUS_BAD_REQUEST, $response->getStatus()); } /** @@ -258,7 +274,7 @@ class AvatarControllerTest extends \Test\TestCase { $response = $this->avatarController->postAvatar(null); //On correct upload always respond with the notsquare message - $this->assertEquals($response->getData()['data'], 'notsquare'); + $this->assertEquals('notsquare', $response->getData()['data']); //File should be deleted $this->assertFalse(file_exists($fileName)); @@ -274,7 +290,7 @@ class AvatarControllerTest extends \Test\TestCase { $response = $this->avatarController->postAvatar(null); - $this->assertEquals($response->getStatus(), Http::STATUS_BAD_REQUEST); + $this->assertEquals(Http::STATUS_BAD_REQUEST, $response->getStatus()); } /** @@ -296,7 +312,7 @@ class AvatarControllerTest extends \Test\TestCase { $response = $this->avatarController->postAvatar(null); - $this->assertEquals($response->getData()['data']['message'], 'Unknown filetype'); + $this->assertEquals('Unknown filetype', $response->getData()['data']['message']); //File should be deleted $this->assertFalse(file_exists($fileName)); @@ -314,7 +330,7 @@ class AvatarControllerTest extends \Test\TestCase { $response = $this->avatarController->postAvatar('avatar.jpg'); //On correct upload always respond with the notsquare message - $this->assertEquals($response->getData()['data'], 'notsquare'); + $this->assertEquals('notsquare', $response->getData()['data']); } /** @@ -323,7 +339,7 @@ class AvatarControllerTest extends \Test\TestCase { public function testPostCroppedAvatarInvalidCrop() { $response = $this->avatarController->postCroppedAvatar([]); - $this->assertEquals($response->getStatus(), Http::STATUS_BAD_REQUEST); + $this->assertEquals(Http::STATUS_BAD_REQUEST, $response->getStatus()); } /** @@ -332,7 +348,7 @@ class AvatarControllerTest extends \Test\TestCase { public function testPostCroppedAvatarNoTmpAvatar() { $response = $this->avatarController->postCroppedAvatar(['x' => 0, 'y' => 0, 'w' => 10, 'h' => 10]); - $this->assertEquals($response->getStatus(), Http::STATUS_BAD_REQUEST); + $this->assertEquals(Http::STATUS_BAD_REQUEST, $response->getStatus()); } /** @@ -345,7 +361,7 @@ class AvatarControllerTest extends \Test\TestCase { $this->container['AvatarManager']->method('getAvatar')->willReturn($this->avatarMock); $response = $this->avatarController->postCroppedAvatar(['x' => 0, 'y' => 0, 'w' => 10, 'h' => 11]); - $this->assertEquals($response->getStatus(), Http::STATUS_BAD_REQUEST); + $this->assertEquals(Http::STATUS_BAD_REQUEST, $response->getStatus()); } /** @@ -356,8 +372,8 @@ class AvatarControllerTest extends \Test\TestCase { $this->container['AvatarManager']->method('getAvatar')->willReturn($this->avatarMock); $response = $this->avatarController->postCroppedAvatar(['x' => 0, 'y' => 0, 'w' => 10, 'h' => 10]); - $this->assertEquals($response->getStatus(), Http::STATUS_OK); - $this->assertEquals($response->getData()['status'], 'success'); + $this->assertEquals(Http::STATUS_OK, $response->getStatus()); + $this->assertEquals('success', $response->getData()['status']); } } diff --git a/tests/data/db_structure.xml b/tests/data/db_structure.xml index 858c9ab1002..371da944832 100644 --- a/tests/data/db_structure.xml +++ b/tests/data/db_structure.xml @@ -238,4 +238,59 @@ </declaration> </table> + <table> + <name>*dbprefix*uniconst</name> + <declaration> + + <field> + <name>id</name> + <type>integer</type> + <default>0</default> + <notnull>true</notnull> + <autoincrement>1</autoincrement> + <length>4</length> + </field> + + <!-- Foreign Key storages::numeric_id --> + <field> + <name>storage</name> + <type>integer</type> + <default></default> + <notnull>true</notnull> + <length>4</length> + </field> + + <field> + <name>path_hash</name> + <type>text</type> + <default></default> + <notnull>true</notnull> + <length>32</length> + </field> + + <field> + <name>etag</name> + <type>text</type> + <default></default> + <notnull>false</notnull> + <length>40</length> + </field> + + <index> + <!--<name>fs_storage_path_hash</name>--> + <unique>true</unique> + <field> + <name>storage</name> + <sorting>ascending</sorting> + </field> + <field> + <name>path_hash</name> + <sorting>ascending</sorting> + </field> + </index> + + </declaration> + + </table> + </database> diff --git a/tests/lib/db.php b/tests/lib/db.php index aefbb3624ed..1fb384ca9c6 100644 --- a/tests/lib/db.php +++ b/tests/lib/db.php @@ -32,13 +32,18 @@ class Test_DB extends \Test\TestCase { */ private $table4; + /** + * @var string + */ + private $table5; + protected function setUp() { parent::setUp(); - $dbfile = OC::$SERVERROOT.'/tests/data/db_structure.xml'; + $dbFile = OC::$SERVERROOT.'/tests/data/db_structure.xml'; - $r = '_'.OC_Util::generateRandomBytes(4).'_'; - $content = file_get_contents( $dbfile ); + $r = $this->getUniqueID('_', 4).'_'; + $content = file_get_contents( $dbFile ); $content = str_replace( '*dbprefix*', '*dbprefix*'.$r, $content ); file_put_contents( self::$schema_file, $content ); OC_DB::createDbFromStructure(self::$schema_file); @@ -48,6 +53,7 @@ class Test_DB extends \Test\TestCase { $this->table2 = $this->test_prefix.'cntcts_cards'; $this->table3 = $this->test_prefix.'vcategory'; $this->table4 = $this->test_prefix.'decimal'; + $this->table5 = $this->test_prefix.'uniconst'; } protected function tearDown() { @@ -110,7 +116,7 @@ class Test_DB extends \Test\TestCase { } public function testinsertIfNotExist() { - $categoryentries = array( + $categoryEntries = array( array('user' => 'test', 'type' => 'contact', 'category' => 'Family', 'expectedResult' => 1), array('user' => 'test', 'type' => 'contact', 'category' => 'Friends', 'expectedResult' => 1), array('user' => 'test', 'type' => 'contact', 'category' => 'Coworkers', 'expectedResult' => 1), @@ -118,8 +124,8 @@ class Test_DB extends \Test\TestCase { array('user' => 'test', 'type' => 'contact', 'category' => 'School', 'expectedResult' => 1), ); - foreach($categoryentries as $entry) { - $result = OC_DB::insertIfNotExist('*PREFIX*'.$this->table3, + foreach($categoryEntries as $entry) { + $result = \OCP\DB::insertIfNotExist('*PREFIX*'.$this->table3, array( 'uid' => $entry['user'], 'type' => $entry['type'], @@ -135,14 +141,14 @@ class Test_DB extends \Test\TestCase { } public function testInsertIfNotExistNull() { - $categoryentries = array( + $categoryEntries = array( array('addressbookid' => 123, 'fullname' => null, 'expectedResult' => 1), array('addressbookid' => 123, 'fullname' => null, 'expectedResult' => 0), array('addressbookid' => 123, 'fullname' => 'test', 'expectedResult' => 1), ); - foreach($categoryentries as $entry) { - $result = OC_DB::insertIfNotExist('*PREFIX*'.$this->table2, + foreach($categoryEntries as $entry) { + $result = \OCP\DB::insertIfNotExist('*PREFIX*'.$this->table2, array( 'addressbookid' => $entry['addressbookid'], 'fullname' => $entry['fullname'], @@ -156,14 +162,14 @@ class Test_DB extends \Test\TestCase { $this->assertEquals(2, count($result->fetchAll())); } - public function testinsertIfNotExistDontOverwrite() { - $fullname = 'fullname test'; + public function testInsertIfNotExistDonTOverwrite() { + $fullName = 'fullname test'; $uri = 'uri_1'; $carddata = 'This is a vCard'; // Normal test to have same known data inserted. $query = OC_DB::prepare('INSERT INTO `*PREFIX*'.$this->table2.'` (`fullname`, `uri`, `carddata`) VALUES (?, ?, ?)'); - $result = $query->execute(array($fullname, $uri, $carddata)); + $result = $query->execute(array($fullName, $uri, $carddata)); $this->assertEquals(1, $result); $query = OC_DB::prepare('SELECT `fullname`, `uri`, `carddata` FROM `*PREFIX*'.$this->table2.'` WHERE `uri` = ?'); $result = $query->execute(array($uri)); @@ -174,9 +180,9 @@ class Test_DB extends \Test\TestCase { $this->assertEquals($carddata, $rowset[0]['carddata']); // Try to insert a new row - $result = OC_DB::insertIfNotExist('*PREFIX*'.$this->table2, + $result = \OCP\DB::insertIfNotExist('*PREFIX*'.$this->table2, array( - 'fullname' => $fullname, + 'fullname' => $fullName, 'uri' => $uri, )); $this->assertEquals(0, $result); @@ -192,6 +198,57 @@ class Test_DB extends \Test\TestCase { $this->assertEquals($carddata, $rowset[0]['carddata']); } + public function testInsertIfNotExistsViolating() { + $result = \OCP\DB::insertIfNotExist('*PREFIX*'.$this->table5, + array( + 'storage' => 1, + 'path_hash' => md5('welcome.txt'), + 'etag' => $this->getUniqueID() + )); + $this->assertEquals(1, $result); + + $result = \OCP\DB::insertIfNotExist('*PREFIX*'.$this->table5, + array( + 'storage' => 1, + 'path_hash' => md5('welcome.txt'), + 'etag' => $this->getUniqueID() + ),['storage', 'path_hash']); + + $this->assertEquals(0, $result); + } + + public function insertIfNotExistsViolatingThrows() { + return [ + [null], + [['etag']], + ]; + } + + /** + * @dataProvider insertIfNotExistsViolatingThrows + * @expectedException \Doctrine\DBAL\Exception\UniqueConstraintViolationException + * + * @param array $compareKeys + */ + public function testInsertIfNotExistsViolatingThrows($compareKeys) { + $result = \OCP\DB::insertIfNotExist('*PREFIX*'.$this->table5, + array( + 'storage' => 1, + 'path_hash' => md5('welcome.txt'), + 'etag' => $this->getUniqueID() + )); + $this->assertEquals(1, $result); + + $result = \OCP\DB::insertIfNotExist('*PREFIX*'.$this->table5, + array( + 'storage' => 1, + 'path_hash' => md5('welcome.txt'), + 'etag' => $this->getUniqueID() + ), $compareKeys); + + $this->assertEquals(0, $result); + } + public function testUtf8Data() { $table = "*PREFIX*{$this->table2}"; $expected = "ะรถ้ๅ\xE2\x80\xA2"; diff --git a/tests/lib/files/pathverificationtest.php b/tests/lib/files/pathverificationtest.php index 1a802a48f57..5d38c6291a6 100644 --- a/tests/lib/files/pathverificationtest.php +++ b/tests/lib/files/pathverificationtest.php @@ -23,6 +23,16 @@ class PathVerification extends \Test\TestCase { } /** + * @expectedException \OCP\Files\InvalidPathException + * @expectedExceptionMessage File name is too long + */ + public function testPathVerificationFileNameTooLong() { + $fileName = str_repeat('a', 500); + $this->view->verifyPath('', $fileName); + } + + + /** * @dataProvider providesEmptyFiles * @expectedException \OCP\Files\InvalidPathException * @expectedExceptionMessage Empty filename is not allowed diff --git a/tests/lib/httphelper.php b/tests/lib/httphelper.php index 48d6543f1f2..fe76f984258 100644 --- a/tests/lib/httphelper.php +++ b/tests/lib/httphelper.php @@ -41,60 +41,16 @@ class TestHTTPHelper extends \Test\TestCase { } /** - * Note: Not using a dataprovider because onConsecutiveCalls expects not - * an array but the function arguments directly - */ - public function testGetFinalLocationOfURLValid() { - $url = 'https://www.owncloud.org/enterprise/'; - $expected = 'https://www.owncloud.com/enterprise/'; - $this->httpHelperMock->expects($this->any()) - ->method('getHeaders') - ->will($this->onConsecutiveCalls( - array('Location' => 'http://www.owncloud.com/enterprise/'), - array('Location' => 'https://www.owncloud.com/enterprise/') - )); - $result = $this->httpHelperMock->getFinalLocationOfURL($url); - $this->assertSame($expected, $result); - } - - /** - * Note: Not using a dataprovider because onConsecutiveCalls expects not - * an array but the function arguments directly - */ - public function testGetFinalLocationOfURLInvalid() { - $url = 'https://www.owncloud.org/enterprise/'; - $expected = 'http://www.owncloud.com/enterprise/'; - $this->httpHelperMock->expects($this->any()) - ->method('getHeaders') - ->will($this->onConsecutiveCalls( - array('Location' => 'http://www.owncloud.com/enterprise/'), - array('Location' => 'file://etc/passwd'), - array('Location' => 'http://www.example.com/') - )); - $result = $this->httpHelperMock->getFinalLocationOfURL($url); - $this->assertSame($expected, $result); - } - - /** - * @expectedException \Exception - * @expectedExceptionMessage URL must begin with HTTPS or HTTP. - */ - public function testGetFinalLocationOfURLException() { - $this->httpHelperMock->getFinalLocationOfURL('file://etc/passwd'); - } - - /** * @dataProvider isHttpTestData */ public function testIsHTTP($url, $expected) { $this->assertSame($expected, $this->httpHelperMock->isHTTPURL($url)); } - /** * @dataProvider postParameters */ - public function testassemblePostParameters($parameterList, $expectedResult) { + public function testAssemblePostParameters($parameterList, $expectedResult) { $helper = \OC::$server->getHTTPHelper(); $result = \Test_Helper::invokePrivate($helper, 'assemblePostParameters', array($parameterList)); $this->assertSame($expectedResult, $result); @@ -107,6 +63,4 @@ class TestHTTPHelper extends \Test\TestCase { array(array(), ''), ); } - - } diff --git a/tests/lib/image.php b/tests/lib/image.php index e0009b9710e..0ee517100ad 100644 --- a/tests/lib/image.php +++ b/tests/lib/image.php @@ -144,6 +144,11 @@ class Test_Image extends \Test\TestCase { $this->assertEquals($expected, $img->data()); } + public function testDataNoResource() { + $img = new \OC_Image(); + $this->assertNull($img->data()); + } + /** * @depends testData */ diff --git a/tests/lib/testcase.php b/tests/lib/testcase.php index 1ea3aa13547..2b4540120d2 100644 --- a/tests/lib/testcase.php +++ b/tests/lib/testcase.php @@ -22,10 +22,24 @@ namespace Test; +use OC\Command\QueueBus; use OCP\Security\ISecureRandom; abstract class TestCase extends \PHPUnit_Framework_TestCase { /** + * @var \OC\Command\QueueBus + */ + private $commandBus; + + protected function setUp() { + // overwrite the command bus with one we can run ourselves + $this->commandBus = new QueueBus(); + \OC::$server->registerService('AsyncCommandBus', function(){ + return $this->commandBus; + }); + } + + /** * Returns a unique identifier as uniqid() is not reliable sometimes * * @param string $prefix @@ -55,6 +69,7 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase { /** * Remove all entries from the files map table + * * @param string $dataDir */ static protected function tearDownAfterClassCleanFileMapper($dataDir) { @@ -66,6 +81,7 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase { /** * Remove all entries from the storages table + * * @throws \OC\DatabaseException */ static protected function tearDownAfterClassCleanStorages() { @@ -76,6 +92,7 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase { /** * Remove all entries from the filecache table + * * @throws \OC\DatabaseException */ static protected function tearDownAfterClassCleanFileCache() { @@ -91,11 +108,11 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase { */ static protected function tearDownAfterClassCleanStrayDataFiles($dataDir) { $knownEntries = array( - 'owncloud.log' => true, - 'owncloud.db' => true, - '.ocdata' => true, - '..' => true, - '.' => true, + 'owncloud.log' => true, + 'owncloud.db' => true, + '.ocdata' => true, + '..' => true, + '.' => true, ); if ($dh = opendir($dataDir)) { @@ -122,8 +139,7 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase { $path = $dir . '/' . $file; if (is_dir($path)) { self::tearDownAfterClassCleanStrayDataUnlinkDir($path); - } - else { + } else { @unlink($path); } } @@ -169,4 +185,11 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase { \OC_Util::tearDownFS(); \OC_User::setUserId(''); } + + /** + * Run all commands pushed to the bus + */ + protected function runCommands() { + $this->commandBus->run(); + } } |