Skip to content

Commit

Permalink
0.7.11: Tests now as public and admin
Browse files Browse the repository at this point in the history
  • Loading branch information
classaxe committed Jul 21, 2018
1 parent 62e31c0 commit 8791d10
Show file tree
Hide file tree
Showing 5 changed files with 277 additions and 33 deletions.
115 changes: 115 additions & 0 deletions tests/Base.php
Expand Up @@ -8,17 +8,27 @@ abstract class Base extends WebTestCase
{
protected $client;

protected $currentUserType = 'public';

protected $currentRedirectStatus;

protected function setUp()
{
$client = static::createClient();
$this->client = $client;
}

/**
* @return array
*/
protected function getSystems()
{
return ['reu', 'rna', 'rww'];
}

/**
* @return array
*/
protected function getVisitors()
{
return [
Expand All @@ -30,53 +40,158 @@ protected function getVisitors()
];
}

/**
* @return array
*/
protected function getAdminUsers()
{
return [
'bogus1' => [
'password' => 'password1',
'valid' => false
],
getenv('ADMIN_USER') => [
'password' => getenv('ADMIN_PASS'),
'valid' => true,
],
'bogus2' => [
'password' => 'password2',
'valid' => false
],
];
}

protected function getAdminUser()
{
return [
'user' => getenv('ADMIN_USER'),
'password' => getenv('ADMIN_PASS')
];
}

/**
* @return mixed
*/
protected function getCrawler()
{
return $this->client->getCrawler();
}

/**
* @param $match
* @return mixed
*/
protected function filter($match)
{
return $this->getCrawler()->filter($match);
}

/**
* @param null $id
* @param array $args
* @return string
*/
protected function getError($id = null, $args = [])
{
return sprintf('ERROR '.static::MESSAGES[$id], ...$args);
}

/**
* @return mixed
*/
protected function getResponse()
{
return $this->client->getResponse();
}

/**
* @return mixed
*/
protected function getResponseContent()
{
return $this->getResponse()->getContent();
}

/**
* @return mixed
*/
protected function getResponseRedirectLocation()
{
return $this->getResponse()->headers->get('location');
}

/**
* @return mixed
*/
protected function getResponsePageTitle()
{
return $this->getCrawler()->filter('title')->eq(0)->text();
}

/**
* @return mixed
*/
protected function getResponseStatusCode()
{
return $this->getResponse()->getStatusCode();
}

/**
* @return $this
*/
protected function setNoRedirect()
{
$this->currentRedirectStatus = false;
$this->client->setMaxRedirects(1);
$this->client->followRedirects(false);
return $this;
}

/**
* @return $this
*/
protected function setYesRedirect()
{
$this->currentRedirectStatus = true;
$this->client->setMaxRedirects(10);
$this->client->followRedirects(true);
return $this;
}

protected function setUserAdmin()
{
$initialRedirectStatus = $this->currentRedirectStatus;
$this->setYesRedirect();
$admin = $this->getAdminUser();
$this->client->request('GET', '/rww/logon');
$form = $this
->getCrawler()
->filter('button#form_submit')
->form(
[
'form[user]' => $admin['user'],
'form[password]' => $admin['password']
],
'POST'
);
$this->client->submit($form);
if (!$initialRedirectStatus) {
$this->setNoRedirect();
}
$this->currentUserType = 'admin';

}

protected function setUserPublic()
{
$this->client->request('GET', '/rww/logoff');
$this->currentUserType = 'public';
}

/**
* @param $IP
* @return $this
*/
protected function setVisitorIP($IP)
{
putenv('PHPUNIT_CLIENT_IP='.$IP);
Expand Down
23 changes: 20 additions & 3 deletions tests/Controller/DefaultModeTest.php
Expand Up @@ -7,6 +7,12 @@

class DefaultModeTest extends Base
{
const MESSAGES = [
1 => "Testing /%s/ Expected page title '%s', saw '%s'.",
2 => "Testing /%s/ Expected HTTP response code %s, saw %s.",
3 => "Testing /%s/ Expected redirect path %s, saw %s.",
];

protected function getDefaultMode()
{
return 'signal_list';
Expand All @@ -19,9 +25,20 @@ public function test()

$this->client->request('GET', '/'.$system.'/');

$this->assertEquals('Redirecting to /'.$system.'/'.$this->getDefaultMode(), $this->getResponsePageTitle());
$this->assertEquals(302, $this->getResponseStatusCode());
$this->assertEquals('/'.$system.'/'.$this->getDefaultMode(), $this->getResponseRedirectLocation());
$expected = 'Redirecting to /'.$system.'/'.$this->getDefaultMode();
$actual = $this->getResponsePageTitle();
$message = $this->getError(1, [$system, $expected, $actual]);
$this->assertEquals($expected, $actual, $message);

$expected = 302;
$actual = $this->getResponseStatusCode();
$message = $this->getError(2, [$system, $expected, $actual]);
$this->assertEquals($expected, $actual, $message);

$expected = '/'.$system.'/'.$this->getDefaultMode();
$actual = $this->getResponseRedirectLocation();
$message = $this->getError(3, [$system, $expected, $actual]);
$this->assertEquals($expected, $actual, $message);
}
}
}
23 changes: 20 additions & 3 deletions tests/Controller/DefaultSystemTest.php
Expand Up @@ -7,6 +7,12 @@

class DefaultSystemTest extends Base
{
const MESSAGES = [
1 => "Testing / Expected page title '%s', saw '%s'.",
2 => "Testing / Expected HTTP response code %s, saw %s.",
3 => "Testing / Expected redirect path %s, saw %s.",
];

public function test()
{
foreach ($this->getVisitors() as $country => $profile) {
Expand All @@ -16,9 +22,20 @@ public function test()

$this->client->request('GET', '/');

$this->assertEquals('Redirecting to /'.$profile['system'].'/', $this->getResponsePageTitle());
$this->assertEquals(302, $this->getResponseStatusCode());
$this->assertEquals('/'.$profile['system'].'/', $this->getResponseRedirectLocation());
$expected = 'Redirecting to /'.$profile['system'].'/';
$actual = $this->getResponsePageTitle();
$message = $this->getError(1, [$expected, $actual]);
$this->assertEquals($expected, $actual, $message);

$expected = 302;
$actual = $this->getResponseStatusCode();
$message = $this->getError(2, [$expected, $actual]);
$this->assertEquals($expected, $actual, $message);

$expected = '/'.$profile['system'].'/';
$actual = $this->getResponseRedirectLocation();
$message = $this->getError(3, [$expected, $actual]);
$this->assertEquals($expected, $actual, $message);
}
}
}
68 changes: 41 additions & 27 deletions tests/Controller/ListenerListTest.php
Expand Up @@ -8,48 +8,62 @@
class ListenerListTest extends Base
{
const MESSAGES = [
1 => "Testing %s/listeners: Expected HTTP response code %s, saw %s.",
2 => "Testing %s/listeners: Expected page title '%s', saw '%s'.",
3 => "Testing %s/listeners: Expected %s region selector(s), saw %s.",
4 => "Testing %s/listeners: Expected %s result column(s), saw %s.",
5 => "Testing %s/listeners: Expected greater than %s result row(s), saw %s.",
6 => "Testing %s/listeners: Expected %s result column(s), saw %s.",
7 => "Testing %s/listeners: Expected message '%s', saw '%s'.",
1 => "Testing %s/listeners as %s: Expected HTTP response code %s, saw %s.",
2 => "Testing %s/listeners as %s: Expected page title '%s', saw '%s'.",
3 => "Testing %s/listeners as %s: Expected %s region selector(s), saw %s.",
4 => "Testing %s/listeners as %s: Expected %s result column(s), saw %s.",
5 => "Testing %s/listeners as %s: Expected greater than %s result row(s), saw %s.",
6 => "Testing %s/listeners as %s: Expected %s result column(s), saw %s.",
7 => "Testing %s/listeners as %s: Expected message '%s', saw '%s'.",
];

const COLS_MIN = 14; // Only NDBs selected
const COLS_MAX = 20; // All types selected
const COLS_MIN = 14; // Only NDBs selected
const COLS_MAX = 20; // All types selected
const COLS_ADMIN = 4; // Additional colunms for admin users

public function test()
public function testPublic()
{
$this->setUserPublic();
$this->common('public');
}

public function testAdmin()
{
$this->setUserAdmin();
$this->common('admin');
$this->setUserPublic();
}

private function common($usertype = false)
{
foreach ($this->getSystems() as $system) {
$this->client->request('GET', '/'.$system.'/listeners');
$expected = 200;
$actual = $this->getResponseStatusCode();
$message = $this->getError(1, [$system, $expected, $actual]);
$this->client->request('GET', '/' . $system . '/listeners');
$expected = 200;
$actual = $this->getResponseStatusCode();
$message = $this->getError(1, [$system, $usertype, $expected, $actual]);
$this->assertEquals($expected, $actual, $message);

$expected = strToUpper($system).' > Listeners List';
$actual = $this->getResponsePageTitle();
$message = $this->getError(2, [$system, $expected, $actual]);
$expected = strToUpper($system) . ' > Listeners List';
$actual = $this->getResponsePageTitle();
$message = $this->getError(2, [$system, $usertype, $expected, $actual]);
$this->assertEquals($expected, $actual, $message);

$selectors = $this->getCrawler()->filter('#form_region');
$expected = $system==='rww' ? 1 : 0;
$actual = $selectors->count();
$message = $this->getError(3, [$system, $expected, $actual]);
$selectors = $this->getCrawler()->filter('#form_region');
$expected = $system === 'rww' ? 1 : 0;
$actual = $selectors->count();
$message = $this->getError(3, [$system, $usertype, $expected, $actual]);
$this->assertEquals($expected, $actual, $message);

$headRow = $this->getCrawler()->filter('table.listener.results thead tr')->eq(0);
$expected = static::COLS_MIN;
$expected = static::COLS_MIN + ($usertype == 'admin' ? static::COLS_ADMIN : 0);
$actual = $headRow->children()->count();
$message = $this->getError(4, [$system, $expected, $actual]);
$message = $this->getError(4, [$system, $usertype, $expected, $actual]);
$this->assertEquals($expected, $actual, $message);

$resultRows = $this->getCrawler()->filter('table.listener.results tbody')->eq(0);
$expected = 100; // 'GreaterThan' clause is used to test
$actual = $resultRows->children()->count();
$message = $this->getError(5, [$system, $expected, $actual]);
$message = $this->getError(5, [$system, $usertype, $expected, $actual]);
$this->assertGreaterThan($expected, $actual, $message);

$form = $this
Expand All @@ -59,9 +73,9 @@ public function test()
$this->client->submit($form);

$headRow = $this->getCrawler()->filter('table.listener.results thead tr')->eq(0);
$expected = static::COLS_MAX;
$expected = static::COLS_MAX + ($usertype == 'admin' ? static::COLS_ADMIN : 0);
$actual = $headRow->children()->count();
$message = $this->getError(6, [$system, $expected, $actual]);
$message = $this->getError(6, [$system, $usertype, $expected, $actual]);
$this->assertEquals($expected, $actual, $message);

$form = $this
Expand All @@ -73,7 +87,7 @@ public function test()
$noResults = $this->getCrawler()->filter('p.no-results')->eq(0);
$expected = '(No listeners found matching your criteria)';
$actual = $noResults->text();
$message = $this->getError(7, [$system, $expected, $actual]);
$message = $this->getError(7, [$system, $usertype, $expected, $actual]);
$this->assertEquals($expected, $actual, $message);
}
}
Expand Down

0 comments on commit 8791d10

Please sign in to comment.