Skip to content

Commit

Permalink
update last commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mrakodol committed Oct 30, 2014
2 parents c69b79a + 5c254cc commit c9d8fb0
Show file tree
Hide file tree
Showing 6 changed files with 268 additions and 4 deletions.
4 changes: 2 additions & 2 deletions app/controllers/InstallController.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,8 @@ private function seed()
'rule' => ''),
array('varname' => 'copyright',
'groupname' => 'general',
'value' => 'yoursite.com © 2013',
'defaultvalue' => 'A2Z CMS 2013',
'value' => 'yoursite.com © '.date("Y"),
'defaultvalue' => 'A2Z CMS 2013-2014',
'type' =>'text',
'rule' => ''),
array('varname' => 'metadesc',
Expand Down
27 changes: 27 additions & 0 deletions app/tests/blueprints/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

use Woodling\Woodling;
use J20\Uuid;
use Carbon\Carbon;

Woodling::seed('UserAdmin', array('class' => 'User', 'do' => function($blueprint)
{
$blueprint->username = 'admin';
$blueprint->email = '[email protected]';
$blueprint->confirmation_code = md5( uniqid(mt_rand(), true) );
$blueprint->confirmed = 1;
$blueprint->created = Carbon::now();
$blueprint->updated = Carbon::now()->addMonths(2);
$blueprint->role = function() { return Woodling::retrieve('RoleAdmin'); };
}));

Woodling::seed('UserUser', array('class' => 'User', 'do' => function($blueprint)
{
$blueprint->username = 'user';
$blueprint->email = '[email protected]';
$blueprint->confirmation_code = md5( uniqid(mt_rand(), true) );
$blueprint->confirmed = 1;
$blueprint->created = Carbon::now();
$blueprint->updated = Carbon::now()->addMonths(2);
$blueprint->role = function() { return Woodling::retrieve('RoleComment'); };
}));
72 changes: 72 additions & 0 deletions app/tests/controllers/UserControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

class UserControllerTest extends BaseControllerTestCase {

public function testShouldLogin()
{
$this->requestAction('GET', 'UserController@getLogin');
$this->assertRequestOk();
}

public function testShouldDoLogin()
{
$credentials = array(
'email'=>'[email protected]',
'password'=>'admin',
'csrf_token' => Session::getToken()
);

$this->withInput( $credentials )
->requestAction('POST', 'UserController@postLogin');

$this->assertRedirection( URL::action('BlogController@getIndex') );
}

public function testShouldNotDoLoginWhenWrong()
{
$credentials = array(
'email'=>'[email protected]',
'password'=>'wrong',
'csrf_token' => Session::getToken());

$this->withInput( $credentials )
->requestAction('POST', 'UserController@postLogin');

$this->assertRedirection( URL::action('UserController@getLogin') );
}

/**
* @expectedException \Illuminate\Session\TokenMismatchException
*/
public function testShouldNotDoLoginWhenTokenWrong()
{
$credentials = array(
'email'=>'[email protected]',
'password'=>'admin',
'csrf_token' => ''
);

$this->withInput( $credentials )
->requestAction('POST', 'UserController@postLogin');
}

/**
* Testing redirect with logged in user.
*/
public function testLoginShouldRedirectUser()
{
$credentials = array(
'email'=>'[email protected]',
'password'=>'admin',
'csrf_token' => Session::getToken()
);

$this->withInput( $credentials )
->requestAction('POST', 'UserController@postLogin');

$this->requestAction('GET', 'UserController@getLogin');

$this->assertRedirection( URL::to('/') );
}

}
165 changes: 165 additions & 0 deletions app/tests/library/BaseControllerTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
<?php

use Symfony\Component\HttpKernel\Exception\HttpException;

class BaseControllerTestCase extends TestCase
{

/**
* Will contain the parameters of the next request
*
* @var array
*/
protected $requestInput = array();

/**
* Will the last HttpException caught
*
* @var HttpException
*/
protected $lastException;

/**
* Set session and enable Laravel filters
*
*/
public function setUp()
{
parent::setUp();

// Enable session
Session::start();

// Enable filters
Route::enableFilters();
}

/**
* Request an URL by the action name
*
* @param string $method
* @param string $action
* @param array $params
* @return BaseControllerTestCase this for method chaining.
*/
public function requestAction($method, $action, $params = array())
{
$action_url = URL::action($action, $params);

if ($action_url == '')
trigger_error("Action '$action' does not exist");

try {
// The following method returns Synfony's DomCrawler
// but it will not be used when testing controllers
$this->client->request($method, $action_url, array_merge($params, $this->requestInput));
} catch (HttpException $e) {
// Store the HttpException in order to check it later
$this->lastException = $e;
}

return $this; // for method chaining
}

/**
* Set the post parameters and return this for chainable
* method call
*
* @param array $params Post paratemers array.
* @return mixed this.
*/
public function withInput($params)
{
$this->requestInput = $params;

return $this;
}

/**
* Asserts if the status code is correct
*
* @param $code Correct status code
* @return void
*/
public function assertStatusCode($code)
{
if ($this->lastException) {
$realCode = $this->lastException->getStatusCode();
} else {
$realCode = $this->client->getResponse()->getStatusCode();
}

$this->assertEquals($code, $realCode, "Response was not $code, status code was $realCode");
}

/**
* Asserts if the request was Ok (200)
*
* @return void
*/
public function assertRequestOk()
{
$this->assertStatusCode(200);
}

/**
* Asserts if page was redirected correctly
*
* @param $location Location where it should be redirected
* @return void
*/
public function assertRedirection($location = null)
{
$response = $this->client->getResponse();

if ($this->lastException) {
$statusCode = $this->lastException->getStatusCode();
} else {
$statusCode = $response->getStatusCode();
}

$isRedirection = in_array($statusCode, array(201, 301, 302, 303, 307, 308));

$this->assertTrue($isRedirection, "Last request was not a redirection. Status code was " . $statusCode);

if ($location) {
if (!strpos($location, '://'))
$location = 'http://:' . $location;

$this->assertEquals($this->cleanTrailingSlash($location), $this->cleanTrailingSlash($response->headers->get('Location')), 'Page was not redirected to the correct place');
}

}

/**
* Asserts if the session variable is correct
*
* @param string $name Session variable name.
* @param mixed $value Session variable value.
* @return void.
*/
public function assertSessionHas($name, $value = null)
{
$this->assertTrue(Session::has($name), "Session doesn't contain '$name'");

if ($value) {
$this->assertContains($value, Session::get($name), "Session '$name' are not equal to $value");
}
}

public function assertEqualsUrlPath($url, $value = null)
{
$path = parse_url($url, PHP_URL_PATH);
$pathWithoutSlash = substr($path, 1);
$this->assertEquals($pathWithoutSlash, $value);
}

public function cleanTrailingSlash($string)
{
if (substr($string, -1) == '/') {
$string = substr($string, 0, -1);
}

return $string;
}
}
2 changes: 1 addition & 1 deletion app/views/admin/layouts/default.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
<div class="clearfix"></div>
<footer>
<p>
<span style="text-align:left;float:left">&copy; 2013 <a href="#">A2Z CMS</a></span>
<span style="text-align:left;float:left">&copy; 2013-<?=date("Y")?> <a href="#">A2Z CMS</a></span>
<span class="hidden-phone" style="text-align:right;float:right">Powered by: <a href="http://laravel.com/" alt="Laravel 4">Laravel 4</a></span>
</p>

Expand Down
2 changes: 1 addition & 1 deletion app/views/site/layouts/default.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
<div class="row">
<div class="col-lg-12">
<span style="text-align:left;float:left">
&copy; 2013 <a class="a2zcms" href="#">A2Z CMS</a></span>
&copy; 2013-<?=date("Y")?> <a class="a2zcms" href="#">A2Z CMS</a></span>
<span style="text-align: center;padding-left: 30%">{{$copyright}}</span>
<span style="text-align:right;float:right">
Powered by: <a class="a2zcms" href="http://laravel.com/" alt="Laravel 4">Laravel 4</a></span>
Expand Down

0 comments on commit c9d8fb0

Please sign in to comment.