diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..3f62603
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+# due to the different Travis-CI PHP versions we MUST NOT lock to specific versions
+composer.lock
+vendor
diff --git a/.travis.yml b/.travis.yml
index e1d1ca4..d3913c7 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,15 +1,22 @@
-sudo: required
+sudo: false
language: php
php:
- 5.6
- 7.0
+ - 7.1
+ - 7.2
+
+cache:
+ directories:
+ - $HOME/.composer/cache/files
+
+services:
+ - mysql
+ - postgresql
addons:
apt:
packages:
- - mysql-server-5.6
- - mysql-client-core-5.6
- - mysql-client-5.6
- php5-pgsql
- libxml2-utils
@@ -17,71 +24,62 @@ env:
global:
- PHP_COVERAGE=FALSE
matrix:
- - CORE_FORK_BRANCH=nc_master DB=sqlite
- - CORE_FORK_BRANCH=nc_stable11 DB=sqlite
- - CORE_FORK_BRANCH=nc_stable12 DB=sqlite
- - CORE_FORK_BRANCH=oc_master DB=sqlite
- - CORE_FORK_BRANCH=oc_stable10 DB=sqlite
- - CORE_FORK_BRANCH=nc_master DB=mysql
- - CORE_FORK_BRANCH=nc_stable11 DB=mysql
- - CORE_FORK_BRANCH=nc_stable12 DB=mysql
- - CORE_FORK_BRANCH=oc_master DB=mysql
- - CORE_FORK_BRANCH=oc_stable10 DB=mysql
+ - CORE_TYPE=nextcloud CORE_BRANCH=master DB=sqlite
+ - CORE_TYPE=nextcloud CORE_BRANCH=stable12 DB=sqlite
+ - CORE_TYPE=nextcloud CORE_BRANCH=stable11 DB=sqlite
+
+ - CORE_TYPE=nextcloud CORE_BRANCH=master DB=mysql
+ - CORE_TYPE=nextcloud CORE_BRANCH=stable12 DB=mysql
+ - CORE_TYPE=nextcloud CORE_BRANCH=stable11 DB=mysql
+
+ - CORE_TYPE=nextcloud CORE_BRANCH=master DB=pgsql
+ - CORE_TYPE=nextcloud CORE_BRANCH=stable12 DB=pgsql
+ - CORE_TYPE=nextcloud CORE_BRANCH=stable11 DB=pgsql
+
+ - CORE_TYPE=owncloud CORE_BRANCH=master DB=sqlite
+ - CORE_TYPE=owncloud CORE_BRANCH=stable10 DB=sqlite
+
+ - CORE_TYPE=owncloud CORE_BRANCH=master DB=mysql
+ - CORE_TYPE=owncloud CORE_BRANCH=stable10 DB=mysql
+
+ - CORE_TYPE=owncloud CORE_BRANCH=master DB=pgsql
+ - CORE_TYPE=owncloud CORE_BRANCH=stable10 DB=pgsql
matrix:
- allow_failures:
- - php: 7.0
fast_finish: true
-
-branches:
- only:
- - master
+ exclude:
+ # Nextcloud 11/12 are not compatible with PHP 7.2
+ - php: 7.2
+ env: CORE_TYPE=nextcloud CORE_BRANCH=stable11 DB=mysql
+ - php: 7.2
+ env: CORE_TYPE=nextcloud CORE_BRANCH=stable12 DB=mysql
+ - php: 7.2
+ env: CORE_TYPE=nextcloud CORE_BRANCH=stable11 DB=sqlite
+ - php: 7.2
+ env: CORE_TYPE=nextcloud CORE_BRANCH=stable12 DB=sqlite
+ - php: 7.2
+ env: CORE_TYPE=nextcloud CORE_BRANCH=stable11 DB=pgsql
+ - php: 7.2
+ env: CORE_TYPE=nextcloud CORE_BRANCH=stable12 DB=pgsql
before_install:
- - php --info
-
- # Set up DB
- - if [[ "$DB" == 'pgsql' ]]; then createuser -U travis -s oc_autotest; fi
- - if [[ "$DB" == 'mysql' ]]; then mysql -u root -e 'create database oc_autotest;'; fi
- - if [[ "$DB" == 'mysql' ]]; then mysql -u root -e "CREATE USER 'oc_autotest'@'localhost' IDENTIFIED BY '';"; fi
- - if [[ "$DB" == 'mysql' ]]; then mysql -u root -e "GRANT ALL ON oc_autotest.* TO 'oc_autotest'@'localhost';"; fi
-
- - cd ..
- - if [[ $(echo $CORE_FORK_BRANCH | awk -F '_' '{print $1}') == 'oc' ]]; then git clone https://github.com/owncloud/core.git --recursive --depth 1 -b $(echo $CORE_FORK_BRANCH | awk -F '_' '{print $2}') core; fi
- - if [[ $(echo $CORE_FORK_BRANCH | awk -F '_' '{print $1}') == 'nc' ]]; then git clone https://github.com/nextcloud/server.git --recursive --depth 1 -b $(echo $CORE_FORK_BRANCH | awk -F '_' '{print $2}') core; fi
- - mv registration core/apps/
-
-before_script:
- # Set up core
- - php -f core/occ maintenance:install --database-name oc_autotest --database-user oc_autotest --admin-user admin --admin-pass admin --database $DB --database-pass=''
-
- # Set up app
- - php -f core/occ app:enable registration
- # Enable app twice to check occ errors of registered commands
- - php -f core/occ app:enable registration
- - cd core/apps/registration
-
# XDebug is only needed if we report coverage -> speeds up other builds
- if [[ "$PHP_COVERAGE" = "FALSE" ]]; then phpenv config-rm xdebug.ini; fi
+install:
+ - . ./tests/scripts/core-download.sh
+ - . ${BUILD_APP_MODULE_DIR}/tests/scripts/core-database.sh
+ - . ${BUILD_APP_MODULE_DIR}/tests/scripts/core-setup.sh
+
script:
# Check info.xml schema validity
- - wget https://apps.nextcloud.com/schema/apps/info.xsd
- - xmllint appinfo/info.xml --schema info.xsd --noout
- - rm info.xsd
+ - . ${BUILD_APP_MODULE_DIR}/tests/scripts/lint-appinfo.sh
# Check PHP syntax errors
- #- find . -name \*.php -not -path './vendor/*' -exec php -l "{}" \;
-
- # Run server's app code checker
- # TODO: enable once table renames are possible
- # - php ../../occ app:check-code mail
+ - . ${BUILD_APP_MODULE_DIR}/tests/scripts/lint-php.sh
+ # Run app code checker but do not fail, as we have to use private API for now
+ - . ${BUILD_APP_MODULE_DIR}/tests/scripts/app-check-code.sh || true
# Run PHP tests
- - cd tests
- - phpunit -c phpunit.xml
-
- # Test packaging
- #- cd ..
- #- if [[ "$PACKAGE" = "TRUE" ]]; then make appstore; fi
+ - . ${BUILD_APP_MODULE_DIR}/tests/scripts/phpunit.sh
diff --git a/README.md b/README.md
index c3e6395..4c770f5 100644
--- a/README.md
+++ b/README.md
@@ -1,3 +1,5 @@
+[](https://travis-ci.org/buffcode/registration)
+
# Registration
This app allows users to register a new account.
diff --git a/appinfo/info.xml b/appinfo/info.xml
index 9f35631..4127aee 100644
--- a/appinfo/info.xml
+++ b/appinfo/info.xml
@@ -3,10 +3,13 @@
xsi:noNamespaceSchemaLocation="https://apps.nextcloud.com/schema/apps/info.xsd">
diff --git a/tests/scripts/app-check-code.sh b/tests/scripts/app-check-code.sh new file mode 100644 index 0000000..8261e61 --- /dev/null +++ b/tests/scripts/app-check-code.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +echo "travis_fold:start:app.check-code" + +set -e + +php -f ${BUILD_CORE_DIR}/occ app:check-code registration + +echo "travis_fold:end:app.check-code" diff --git a/tests/scripts/core-database.sh b/tests/scripts/core-database.sh new file mode 100755 index 0000000..9bc9fca --- /dev/null +++ b/tests/scripts/core-database.sh @@ -0,0 +1,23 @@ +#!/usr/bin/env bash + +echo "travis_fold:start:core.database.setup" + +set -e + +# MySQL +if [[ "${DB}" == 'mysql' ]] +then + mysql -u root -e 'CREATE DATABASE oc_autotest;' + mysql -u root -e "CREATE USER 'oc_autotest'@'localhost' IDENTIFIED BY '';" + mysql -u root -e "GRANT ALL ON oc_autotest.* TO 'oc_autotest'@'localhost';" +fi + +# Postgres +if [[ "${DB}" == 'pgsql' ]] +then + psql -U postgres -c 'CREATE DATABASE oc_autotest;' + psql -U postgres -c "CREATE USER oc_autotest WITH PASSWORD '';" + psql -U postgres -c 'GRANT ALL PRIVILEGES ON DATABASE oc_autotest TO oc_autotest;' +fi + +echo "travis_fold:end:core.database.setup" diff --git a/tests/scripts/core-download.sh b/tests/scripts/core-download.sh new file mode 100644 index 0000000..945b387 --- /dev/null +++ b/tests/scripts/core-download.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash + +echo "travis_fold:start:core.download" + +set -e + +# export some generic paths +export BUILD_ROOT_DIR=$(dirname `pwd`) +export BUILD_CORE_DIR="${BUILD_ROOT_DIR}/core" +export BUILD_APPS_DIR="${BUILD_CORE_DIR}/apps" +export BUILD_APP_MODULE_DIR="${BUILD_APPS_DIR}/registration" + +if [[ ${CORE_TYPE} == 'owncloud' ]]; then git clone https://github.com/owncloud/core.git --recursive --depth 1 -b ${CORE_BRANCH} ${BUILD_CORE_DIR}; fi +if [[ ${CORE_TYPE} == 'nextcloud' ]]; then git clone https://github.com/nextcloud/server.git --recursive --depth 1 -b ${CORE_BRANCH} ${BUILD_CORE_DIR}; fi + +mv ${BUILD_ROOT_DIR}/registration ${BUILD_APPS_DIR} + +echo "travis_fold:end:core.download" diff --git a/tests/scripts/core-setup.sh b/tests/scripts/core-setup.sh new file mode 100755 index 0000000..5481f53 --- /dev/null +++ b/tests/scripts/core-setup.sh @@ -0,0 +1,20 @@ +#!/usr/bin/env bash + +echo "travis_fold:start:core.setup" + +set -e + +cd ${BUILD_CORE_DIR} + +if [[ ${CORE_TYPE} == 'owncloud' ]]; then composer install -o --prefer-dist --no-suggest --no-interaction; fi + +# Set up core +php -f occ maintenance:install --database-name oc_autotest --database-user oc_autotest --admin-user admin --admin-pass admin --database ${DB} --database-pass='' + +# Set up app +php -f occ app:enable registration + +# Enable app twice to check occ errors of registered commands +php -f occ app:enable registration + +echo "travis_fold:end:core.setup" diff --git a/tests/scripts/lint-appinfo.sh b/tests/scripts/lint-appinfo.sh new file mode 100755 index 0000000..89fb3c1 --- /dev/null +++ b/tests/scripts/lint-appinfo.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +echo "travis_fold:start:lint.appinfo" + +set -e + +cd ${BUILD_APP_MODULE_DIR} + +wget -nv https://apps.nextcloud.com/schema/apps/info.xsd +xmllint appinfo/info.xml --schema info.xsd --noout +rm info.xsd + +echo "travis_fold:end:lint.appinfo" diff --git a/tests/scripts/lint-php.sh b/tests/scripts/lint-php.sh new file mode 100755 index 0000000..afa2a6e --- /dev/null +++ b/tests/scripts/lint-php.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +echo "travis_fold:start:lint.php" + +set -e + +cd ${BUILD_APP_MODULE_DIR} + +find . -name '*.php' -type f -not -path './vendor/*' -print0 | xargs --no-run-if-empty -0 -n1 -P8 php -l -d display_errors -d display_startup_errors 1>/dev/null + +echo "travis_fold:end:lint.php" diff --git a/tests/scripts/phpunit.sh b/tests/scripts/phpunit.sh new file mode 100755 index 0000000..0f92356 --- /dev/null +++ b/tests/scripts/phpunit.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash + +echo "travis_fold:start:phpunit" + +set -e + +cd ${BUILD_APP_MODULE_DIR} + +composer install --prefer-dist --no-interaction -o +vendor/bin/phpunit -c tests/phpunit.xml + +echo "travis_fold:end:phpunit" diff --git a/tests/unit/controller/ApiControllerTest.php b/tests/unit/controller/ApiControllerTest.php index c466e10..0db35a4 100644 --- a/tests/unit/controller/ApiControllerTest.php +++ b/tests/unit/controller/ApiControllerTest.php @@ -14,11 +14,9 @@ namespace OCA\Registration\Controller; use OCA\Registration\Db\Registration; use OCA\Registration\Service\MailService; use OCA\Registration\Service\RegistrationService; -use OCP\AppFramework\Db\DoesNotExistException; +use OCA\Registration\Util\CoreBridge; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; -use OCP\AppFramework\OCS\OCSException; -use OCP\AppFramework\OCS\OCSNotFoundException; use OCP\Defaults; use OCP\IL10N; use OCP\IRequest; @@ -80,58 +78,58 @@ class ApiControllerTest extends TestCase { $this->assertEquals($expected, $actual); } - /** - * @expectedException \OCP\AppFramework\OCS\OCSException - * @expectedExceptionCode 999 - */ public function testValidateFailEmail() { + $exception = CoreBridge::createException('OCSException', '', 999); + + $this->expectException(get_class($exception)); + $this->registrationService ->expects($this->once()) ->method('validateEmail') - ->willThrowException(new OCSException('', 999)); + ->willThrowException($exception); + $this->controller->validate('user1', 'user test', 'test@example.com'); } - /** - * @expectedException \OCP\AppFramework\OCS\OCSException - * @expectedExceptionCode 999 - */ public function testValidateFailDisplayname() { - $this->registrationService + $exception = CoreBridge::createException('OCSException', '', 999); + + $this->expectException(get_class($exception)); + + $this->registrationService ->expects($this->once()) ->method('validateDisplayname') - ->willThrowException(new OCSException('', 999)); + ->willThrowException($exception); + $this->controller->validate('user1', 'user test', 'test@example.com'); } - /** - * @expectedException \OCP\AppFramework\OCS\OCSException - * @expectedExceptionCode 999 - */ public function testValidateFailUsername() { - $this->registrationService + $exception = CoreBridge::createException('OCSException', '', 999); + + $this->expectException(get_class($exception)); + + $this->registrationService ->expects($this->once()) ->method('validateUsername') - ->willThrowException(new OCSException('', 999)); + ->willThrowException($exception); + $this->controller->validate('user1', 'user test', 'test@example.com'); } - /** - * @expectedException \OCP\AppFramework\OCS\OCSNotFoundException - * @expectedExceptionCode 404 - */ public function testStatusNoRegistration() { + $exception = CoreBridge::createException('OCSNotFoundException', '', 404); + + $this->expectException(get_class($exception)); + $this->registrationService ->method('getRegistrationForSecret') ->with('L2qdLAtrJTx499ErjwkwnZqGmLdm3Acp') - ->willThrowException(new DoesNotExistException('')); + ->willThrowException($exception); + $this->controller->status('L2qdLAtrJTx499ErjwkwnZqGmLdm3Acp'); } - /** - * @expectedException \OCP\AppFramework\OCS\OCSException - * @expectedExceptionCode 403 - */ public function testStatusPendingRegistration() { $registration = new Registration(); $registration->setEmailConfirmed(false); @@ -139,7 +137,18 @@ class ApiControllerTest extends TestCase { ->method('getRegistrationForSecret') ->with('L2qdLAtrJTx499ErjwkwnZqGmLdm3Acp') ->willReturn($registration); + $actual = $this->controller->status('L2qdLAtrJTx499ErjwkwnZqGmLdm3Acp'); + + $expected = new DataResponse( + [ + 'registrationStatus' => 1, + 'message' => $this->l10n->t('Your registration is pending. Please confirm your email address.'), + ], + Http::STATUS_OK + ); + + $this->assertEquals($expected, $actual); } public function testStatusConfirmedRegistration() { @@ -163,12 +172,11 @@ class ApiControllerTest extends TestCase { ->expects($this->once()) ->method('generateAppPassword'); $actual = $this->controller->status('mysecret'); - $expected = new DataResponse([]); + $expected = new DataResponse([ + 'appPassword' => null, + 'cloudUrl' => $this->defaults->getBaseUrl(), + 'registrationStatus' => 0, + ]); $this->assertEquals($expected, $actual); } - - public function testStatusConfirmedRegistrationWithSecret() { - - } - } diff --git a/util/corebridge.php b/util/corebridge.php new file mode 100644 index 0000000..2eee5dd --- /dev/null +++ b/util/corebridge.php @@ -0,0 +1,77 @@ + [ + 'nextcloud' => 'OCP\AppFramework\OCS\OCSException', + 'owncloud' => 'OC\OCS\Exception', + ], + 'OCSBadRequestException' => [ + 'nextcloud' => 'OCP\AppFramework\OCS\OCSBadRequestException', + 'owncloud' => 'OC\OCS\Exception', + ], + 'OCSNotFoundException' => [ + 'nextcloud' => 'OCP\AppFramework\OCS\OCSNotFoundException', + 'owncloud' => 'OC\OCS\Exception', + ], + 'DoesNotExistException' => [ + 'nextcloud' => 'OCP\AppFramework\Db\DoesNotExistException', + 'owncloud' => 'OCP\AppFramework\Db\DoesNotExistException', + ], + ]; + + if (!array_key_exists($className, $classes)) { + throw new \LogicException('No valid exception class found'); + } + + foreach ($classes[$className] as $class) { + if (class_exists($class)) { + return $class; + } + } + + throw new \LogicException('No valid exception class found'); + } + + /** + * @param string $className + * @param null|string $message + * @param null|int $code + * @return \Exception + */ + public static function createException($className, $message = null, $code = null) + { + $exceptionClassName = self::exceptionClass($className); + + $reflection = new \ReflectionClass($exceptionClassName); + $params = $reflection->getConstructor()->getParameters(); + + if ($params[0]->getClass() && $params[0]->getClass()->getName() === 'OC\OCS\Result') { + return new $exceptionClassName(new \OC\OCS\Result($message, $code)); + } + + if (count($params) >= 2) { + if ($params[1]->getClass() && $params[1]->getClass()->getName() === 'Exception') { + return new $exceptionClassName($message); + } + + return new $exceptionClassName($message, $code); + } + + if ($exceptionClassName === 'OCP\AppFramework\OCS\OCSNotFoundException') { + return new $exceptionClassName($message); + } + + return new $exceptionClassName(); + } +}