Skip to content

Commit

Permalink
Implement unit testing for this plugin (#328)
Browse files Browse the repository at this point in the history
Huge credit to @scottbedard
  • Loading branch information
scottbedard authored and LukeTowers committed Dec 28, 2018
1 parent cd2de68 commit 2f8c6af
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
composer.lock
vendor
26 changes: 26 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
language: php

php:
- 7.0
- 7.1
- 7.2
- nightly

matrix:
allow_failures:
- php: nightly

before_script:
- wget https://getcomposer.org/composer.phar
- php composer.phar install --no-interaction
- composer self-update
- git clone https://github.com/octobercms/october.git
- mkdir -p ./october/plugins/rainlab/user
- mv !(october) october/plugins/rainlab/user
- cd october
- composer install
- cp config/cms.php config/testing/cms.php
- cd ./plugins/rainlab/user

script:
- php vendor/bin/phpunit
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Front-end user plugin

[![Build Status](https://img.shields.io/travis/givingteam/user-plugin.svg?branch=master)](https://travis-ci.org/givingteam/user-plugin)
[![MIT License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/rainlab/user-plugin/blob/master/LICENCE.md)

Front-end user management for October CMS.

## Requirements
Expand Down
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@
"php": ">=5.5.9",
"composer/installers": "~1.0"
},
"require-dev": {
"phpunit/phpunit": "~5.7"
},
"minimum-stability": "dev"
}
39 changes: 39 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="../../../tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false">
<testsuites>
<testsuite name="rainlab.user">
<directory>./tests</directory>
</testsuite>
</testsuites>
<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
</php>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./classes</directory>
<directory suffix=".php">./components</directory>
<directory suffix=".php">./controllers</directory>
<directory suffix=".php">./facades</directory>
<directory suffix=".php">./models</directory>
<directory suffix=".php">./notifyrules</directory>
<exclude>
<file>./Plugin.php</file>
<directory>./tests</directory>
<directory>./updates</directory>
<directory>./vendor</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
41 changes: 41 additions & 0 deletions tests/PluginTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php namespace RainLab\User\Tests;

use App;
use Artisan;
use Illuminate\Foundation\AliasLoader;
use PluginTestCase as BasePluginTestCase;
use RainLab\User\Models\Settings;

abstract class PluginTestCase extends BasePluginTestCase
{
/**
* @var array Plugins to refresh between tests.
*/
protected $refreshPlugins = [
'RainLab.User',
];

/**
* Perform test case set up.
*
* @return void
*/
public function setUp()
{
parent::setUp();

// reset any modified settings
Settings::resetDefault();

// log out after each test
\RainLab\User\Classes\AuthManager::instance()->logout();

// register the auth facade
$alias = AliasLoader::getInstance();
$alias->alias('Auth', 'RainLab\User\Facades\Auth');

App::singleton('user.auth', function() {
return \RainLab\User\Classes\AuthManager::instance();
});
}
}
84 changes: 84 additions & 0 deletions tests/unit/facades/AuthFacadeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php namespace RainLab\User\Tests\Unit\Facades;

use Auth;
use RainLab\User\Models\User;
use RainLab\User\Tests\PluginTestCase;

class AuthFacadeTest extends PluginTestCase
{
public function test_registering_a_user()
{
// register a user
$user = Auth::register([
'name' => 'Some User',
'email' => '[email protected]',
'password' => 'changeme',
'password_confirmation' => 'changeme',
]);

// our one user should be returned
$this->assertEquals(1, User::count());
$this->assertInstanceOf('RainLab\User\Models\User', $user);

// and that user should have the following data
$this->assertFalse($user->is_activated);
$this->assertEquals('Some User', $user->name);
$this->assertEquals('[email protected]', $user->email);
}

public function test_registering_a_user_with_auto_activation()
{
// register a user with the auto-activate flag
$user = Auth::register([
'name' => 'Some User',
'email' => '[email protected]',
'password' => 'changeme',
'password_confirmation' => 'changeme',
], true);

// that user should be activated
$this->assertTrue($user->is_activated);

// and we should now be authenticated
$this->assertTrue(Auth::check());
}

public function test_registering_a_guest()
{
// register a guest
$guest = Auth::registerGuest(['email' => '[email protected]']);

// our one guest should be returned
$this->assertEquals(1, User::count());
$this->assertInstanceOf('RainLab\User\Models\User', $guest);

// and that guest should have the following data
$this->assertTrue($guest->is_guest);
$this->assertEquals('[email protected]', $guest->email);
}

public function test_login_and_checking_authentication()
{
// we should not be authenticated
$this->assertFalse(Auth::check());

// create a user
$user = User::create([
'name' => 'Some User',
'email' => '[email protected]',
'password' => 'changeme',
'password_confirmation' => 'changeme',
]);

// in order to log in as this user, we must be activated
$user->is_activated = true;
$user->activated_at = now();
$user->save();

// log in as a new user
Auth::login($user);

// we should now be authenticated
$this->assertTrue(Auth::check());
}
}

0 comments on commit 2f8c6af

Please sign in to comment.