Skip to content

Commit

Permalink
added test
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Wagner committed Mar 14, 2018
1 parent 39da088 commit 538bcf3
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/Manager/AjaxTokenManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ public function get()
/**
* Remove a used token.
*
* @param $strToken
* @param $token
*/
public function remove($strToken)
public function remove(string $token): void
{
System::getContainer()->get('huh.utils.array')->removeValue($strToken, $this->tokens);
System::getContainer()->get('huh.utils.array')->removeValue($token, $this->tokens);

$this->session->set(static::SESSION_KEY, $this->tokens);
}
Expand Down Expand Up @@ -110,14 +110,14 @@ public function getActiveToken()
/**
* Validate a token.
*
* @param string $strToken The ajax token
* @param string $token The ajax token
*
* @return bool True if the token matches the stored one
*/
public function validate($strToken)
public function validate(string $token): bool
{
// Validate the token
if ('' !== $strToken && in_array($strToken, $this->tokens, true)) {
if ('' !== $token && in_array($token, $this->tokens, true)) {
return true;
}

Expand Down
107 changes: 107 additions & 0 deletions tests/Manager/AjaxTokenManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

/*
* Copyright (c) 2018 Heimrich & Hannot GmbH
*
* @license LGPL-3.0-or-later
*/

namespace HeimrichHannot\AjaxBundle\Tests\Manager;

use Contao\CoreBundle\Routing\ScopeMatcher;
use Contao\System;
use Contao\TestCase\ContaoTestCase;
use HeimrichHannot\AjaxBundle\Manager\AjaxManager;
use HeimrichHannot\AjaxBundle\Manager\AjaxTokenManager;
use HeimrichHannot\RequestBundle\Component\HttpFoundation\Request;
use HeimrichHannot\UtilsBundle\Arrays\ArrayUtil;
use Symfony\Component\HttpFoundation\RequestMatcher;
use Symfony\Component\HttpFoundation\RequestStack;

class AjaxTokenManagerTest extends ContaoTestCase
{
public function setUp()
{
parent::setUp();

if (!defined('TL_ROOT')) {
define('TL_ROOT', '');
}

$requestStack = new RequestStack();
$requestStack->push(new \Symfony\Component\HttpFoundation\Request());

$backendMatcher = new RequestMatcher('/contao', 'test.com', null, ['192.168.1.0']);
$frontendMatcher = new RequestMatcher('/index', 'test.com', null, ['192.168.1.0']);

$scopeMatcher = new ScopeMatcher($backendMatcher, $frontendMatcher);

$tokenAdapter = $this->mockAdapter(['getToken', 'getValue']);
$tokenAdapter->method('getToken')->willReturnSelf();
$tokenAdapter->method('getValue')->willReturn('token');

$request = new Request($this->mockContaoFramework(), $requestStack, $scopeMatcher);
$request->setGet(AjaxManager::AJAX_ATTR_ACT, 'false');
$request->setGet(AjaxManager::AJAX_ATTR_TOKEN, 'ag');
$request->setGet(AjaxManager::AJAX_ATTR_SCOPE, 'ajax');
$request->setGet(AjaxManager::AJAX_ATTR_GROUP, 'ag');

$container = $this->mockContainer();
$container->set('huh.request', $request);
$container->set('huh.utils.array', new ArrayUtil($this->mockContaoFramework()));
System::setContainer($container);
}

public function testInstantiation()
{
$manager = new AjaxTokenManager();
$this->assertInstanceOf(AjaxTokenManager::class, $manager);
}

public function testGet()
{
$manager = new AjaxTokenManager();
$token = $manager->get();
$this->assertNotNull($token);
$this->assertSame(32, strlen($token[0]));
}

public function testRemove()
{
$manager = new AjaxTokenManager();
$token = $manager->get();
$manager->remove($token[0]);

$newToken = $manager->get();
$this->assertEmpty($newToken);
}

public function testCreate()
{
$manager = new AjaxTokenManager();
$token = $manager->create();
$this->assertSame(32, strlen($token));
}

public function testValidate()
{
$manager = new AjaxTokenManager();
$token = $manager->get();
$this->assertTrue($manager->validate($token[0]));
$this->assertFalse($manager->validate(''));

$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
$GLOBALS['TL_CONFIG']['requestTokenWhitelist'] = ['127.0.0.1', 'localhost'];
$this->assertTrue($manager->validate('token'));
}

public function testGetActiveToken()
{
$manager = new AjaxTokenManager();
$this->assertNull($manager->getActiveToken());

$token = $manager->get();
System::getContainer()->get('huh.request')->setGet(AjaxManager::AJAX_ATTR_TOKEN, $token[0]);
$this->assertSame($token[0], $manager->getActiveToken());
}
}
14 changes: 14 additions & 0 deletions tests/Response/Response404Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,26 @@

namespace HeimrichHannot\AjaxBundle\Tests\Response;

use Contao\System;
use Contao\TestCase\ContaoTestCase;
use HeimrichHannot\AjaxBundle\Response\Response;
use HeimrichHannot\AjaxBundle\Response\Response404;

class Response404Test extends ContaoTestCase
{
public function setUp()
{
parent::setUp();

$token = $this->mockAdapter(['getActiveToken', 'create']);
$token->method('getActiveToken')->willReturn('token');
$token->method('create')->willReturn('token');

$container = $this->mockContainer();
$container->set('huh.ajax.token', $token);
System::setContainer($container);
}

public function testResponse()
{
$response = new Response404('test');
Expand Down
14 changes: 14 additions & 0 deletions tests/Response/ResponseErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,26 @@

namespace HeimrichHannot\AjaxBundle\Tests\Response;

use Contao\System;
use Contao\TestCase\ContaoTestCase;
use HeimrichHannot\AjaxBundle\Response\Response;
use HeimrichHannot\AjaxBundle\Response\ResponseError;

class ResponseErrorTest extends ContaoTestCase
{
public function setUp()
{
parent::setUp();

$token = $this->mockAdapter(['getActiveToken', 'create']);
$token->method('getActiveToken')->willReturn('token');
$token->method('create')->willReturn('token');

$container = $this->mockContainer();
$container->set('huh.ajax.token', $token);
System::setContainer($container);
}

public function testResponse()
{
$response = new ResponseError('test');
Expand Down
14 changes: 14 additions & 0 deletions tests/Response/ResponseRedirectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,26 @@

namespace HeimrichHannot\AjaxBundle\Tests\Response;

use Contao\System;
use Contao\TestCase\ContaoTestCase;
use HeimrichHannot\AjaxBundle\Response\Response;
use HeimrichHannot\AjaxBundle\Response\ResponseRedirect;

class ResponseRedirectTest extends ContaoTestCase
{
public function setUp()
{
parent::setUp();

$token = $this->mockAdapter(['getActiveToken', 'create']);
$token->method('getActiveToken')->willReturn('token');
$token->method('create')->willReturn('token');

$container = $this->mockContainer();
$container->set('huh.ajax.token', $token);
System::setContainer($container);
}

public function testResponse()
{
$response = new ResponseRedirect('test');
Expand Down
14 changes: 14 additions & 0 deletions tests/Response/ResponseSuccessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,26 @@

namespace HeimrichHannot\AjaxBundle\Tests\Response;

use Contao\System;
use Contao\TestCase\ContaoTestCase;
use HeimrichHannot\AjaxBundle\Response\Response;
use HeimrichHannot\AjaxBundle\Response\ResponseSuccess;

class ResponseSuccessTest extends ContaoTestCase
{
public function setUp()
{
parent::setUp();

$token = $this->mockAdapter(['getActiveToken', 'create']);
$token->method('getActiveToken')->willReturn('token');
$token->method('create')->willReturn('token');

$container = $this->mockContainer();
$container->set('huh.ajax.token', $token);
System::setContainer($container);
}

public function testResponse()
{
$response = new ResponseSuccess('test');
Expand Down

0 comments on commit 538bcf3

Please sign in to comment.