2015-05-30 06:22:06 +00:00
|
|
|
<?php
|
|
|
|
|
2017-01-09 19:08:01 +00:00
|
|
|
namespace Tests\Skobkin\PointToolsBundle\Controller;
|
2015-05-30 06:22:06 +00:00
|
|
|
|
2019-04-03 16:19:03 +00:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Client;
|
2015-05-30 06:22:06 +00:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
|
|
|
|
|
|
|
class MainControllerTest extends WebTestCase
|
|
|
|
{
|
2019-04-03 16:38:57 +00:00
|
|
|
public function testUserSearch(): void
|
2015-05-30 06:22:06 +00:00
|
|
|
{
|
|
|
|
$client = static::createClient();
|
|
|
|
$crawler = $client->request('GET', '/');
|
2016-12-11 20:27:14 +00:00
|
|
|
|
|
|
|
$userSearchForm = $crawler->filter('form.form-inline')->form();
|
2017-01-08 21:24:25 +00:00
|
|
|
$userSearchForm['user_search[login]'] = 'testuser';
|
2016-12-11 20:27:14 +00:00
|
|
|
|
2016-12-12 18:37:15 +00:00
|
|
|
$client->submit($userSearchForm);
|
2016-12-11 20:27:14 +00:00
|
|
|
|
|
|
|
$this->assertTrue($client->getResponse()->isRedirect('/user/testuser'), 'Redirect to testuser\'s page didn\'t happen');
|
2015-05-30 06:22:06 +00:00
|
|
|
}
|
|
|
|
|
2019-04-03 16:38:57 +00:00
|
|
|
public function testNonExistingUserSearch(): void
|
2016-12-11 23:58:11 +00:00
|
|
|
{
|
|
|
|
$client = static::createClient();
|
|
|
|
$crawler = $client->request('GET', '/');
|
|
|
|
|
|
|
|
$userSearchForm = $crawler->filter('form.form-inline')->form();
|
2017-01-08 21:24:25 +00:00
|
|
|
$userSearchForm['user_search[login]'] = 'non-existing-user';
|
2016-12-11 23:58:11 +00:00
|
|
|
|
|
|
|
$crawler = $client->submit($userSearchForm);
|
|
|
|
|
|
|
|
$this->assertFalse($client->getResponse()->isRedirection(), 'Redirect to non-existing user on the main page');
|
|
|
|
|
|
|
|
$formElement = $crawler->filter('form.form-inline')->first();
|
|
|
|
|
|
|
|
$this->assertEquals(1, $formElement->count(), 'Form not found after searching non-existing user');
|
|
|
|
|
2017-01-08 21:24:25 +00:00
|
|
|
$loginInputElement = $formElement->filter('#user_search_login')->first();
|
2016-12-11 23:58:11 +00:00
|
|
|
|
|
|
|
$this->assertEquals(1, $loginInputElement->count(), 'Login form input element not found after search of non existing user');
|
|
|
|
|
|
|
|
$errorsListElement = $loginInputElement->siblings()->filter('span.help-block')->children()->filter('ul.list-unstyled')->first();
|
|
|
|
|
|
|
|
$this->assertEquals(1, $errorsListElement->count(), 'Form errors list not found after search of non-existing user');
|
|
|
|
|
|
|
|
$firstError = $errorsListElement->children()->first();
|
|
|
|
|
|
|
|
$this->assertEquals(1, $firstError->count(), 'No errors in the list');
|
|
|
|
$this->assertEquals(' Login not found', $firstError->text(), 'Incorrect error text');
|
|
|
|
}
|
|
|
|
|
2019-04-03 16:38:57 +00:00
|
|
|
public function testUserStats(): void
|
2016-12-11 20:27:14 +00:00
|
|
|
{
|
|
|
|
$client = static::createClient();
|
|
|
|
$crawler = $client->request('GET', '/');
|
|
|
|
|
|
|
|
$userStatsBlock = $crawler->filter('.container.service-stats');
|
|
|
|
|
|
|
|
// Assuming we have stats block
|
|
|
|
$this->assertEquals(1, $userStatsBlock->count(), 'Stats block not found');
|
|
|
|
// @todo rewrite to named classes
|
|
|
|
// Assuming we have at least one user shown
|
|
|
|
$this->assertGreaterThan(
|
|
|
|
0,
|
|
|
|
$userStatsBlock->children()->first()->children()->last()->text(),
|
|
|
|
'Zero service users shown on the main page'
|
|
|
|
);
|
|
|
|
// Assuming we have at least one subscriber
|
|
|
|
$this->assertGreaterThan(
|
|
|
|
0,
|
2016-12-11 21:09:29 +00:00
|
|
|
$userStatsBlock->children()->eq(1)->children()->last()->text(),
|
|
|
|
'Zero service subscribers shows on the main page'
|
2016-12-11 20:27:14 +00:00
|
|
|
);
|
|
|
|
}
|
2016-12-12 00:22:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests AJAX user search autocomplete and returns JSON response string
|
|
|
|
*/
|
2019-04-03 16:19:03 +00:00
|
|
|
public function testAjaxUserAutoComplete(): string
|
2016-12-12 00:22:02 +00:00
|
|
|
{
|
2019-04-03 16:19:03 +00:00
|
|
|
$client = $this->createClientForAjaxUserSearchByLogin('testuser');
|
2016-12-12 00:22:02 +00:00
|
|
|
|
|
|
|
$this->assertTrue($client->getResponse()->headers->contains('Content-Type', 'application/json'), 'Response has "Content-Type" = "application/json"');
|
|
|
|
|
|
|
|
return $client->getResponse()->getContent();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testAjaxUserAutoComplete
|
|
|
|
*/
|
2019-04-03 16:19:03 +00:00
|
|
|
public function testAjaxUserAutoCompleteHasOptions(string $json): array
|
2016-12-12 00:22:02 +00:00
|
|
|
{
|
2019-04-03 16:34:01 +00:00
|
|
|
$data = json_decode($json, true);
|
2016-12-12 00:22:02 +00:00
|
|
|
|
|
|
|
$this->assertNotNull($data, 'JSON data successfully decoded and not empty');
|
|
|
|
$this->assertTrue(is_array($data), 'JSON data is array');
|
2019-04-03 16:19:03 +00:00
|
|
|
$this->assertCount(2, $data, 'Array has 2 elements');
|
2016-12-12 00:22:02 +00:00
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testAjaxUserAutoCompleteHasOptions
|
|
|
|
*/
|
2019-04-03 16:38:57 +00:00
|
|
|
public function testAjaxUserAutoCompleteHasValidUserObjects(array $users): void
|
2016-12-12 00:22:02 +00:00
|
|
|
{
|
|
|
|
foreach ($users as $key => $user) {
|
|
|
|
$this->assertTrue(array_key_exists('login', $user), sprintf('%d row of result has \'login\' field', $key));
|
|
|
|
$this->assertTrue(array_key_exists('name', $user), sprintf('%d row of result has \'name\' field', $key));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-03 16:19:03 +00:00
|
|
|
/**
|
|
|
|
* Tests AJAX user search autocomplete for unnamed user and returns JSON response string
|
|
|
|
*/
|
|
|
|
public function testAjaxUserAutoCompleteForUnnamedUser(): string
|
|
|
|
{
|
|
|
|
$client = $this->createClientForAjaxUserSearchByLogin('unnamed_user');
|
|
|
|
|
|
|
|
$this->assertTrue($client->getResponse()->headers->contains('Content-Type', 'application/json'), 'Response has "Content-Type" = "application/json"');
|
|
|
|
|
|
|
|
return $client->getResponse()->getContent();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-03 16:28:18 +00:00
|
|
|
* @depends testAjaxUserAutoCompleteForUnnamedUser
|
2019-04-03 16:19:03 +00:00
|
|
|
*/
|
|
|
|
public function testAjaxUserAutoCompleteHasOptionsForUnnamedUser(string $json): array
|
|
|
|
{
|
2019-04-03 16:34:01 +00:00
|
|
|
$data = json_decode($json, true);
|
2019-04-03 16:19:03 +00:00
|
|
|
|
|
|
|
$this->assertNotNull($data, 'JSON data successfully decoded and not empty');
|
2019-04-03 16:38:57 +00:00
|
|
|
$this->assertInternalType('array', $data, 'JSON data is array');
|
2019-04-03 16:19:03 +00:00
|
|
|
$this->assertCount(1, $data, 'Array has 1 elements');
|
|
|
|
|
|
|
|
return reset($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-03 16:28:18 +00:00
|
|
|
* @depends testAjaxUserAutoCompleteHasOptionsForUnnamedUser
|
2019-04-03 16:19:03 +00:00
|
|
|
*/
|
2019-04-03 16:38:57 +00:00
|
|
|
public function testAjaxUserAutoCompleteHasValidUserObjectsForUnnamedUser(array $user): void
|
2019-04-03 16:19:03 +00:00
|
|
|
{
|
|
|
|
$this->assertTrue(array_key_exists('login', $user), 'Result has \'login\' field');
|
|
|
|
$this->assertTrue(array_key_exists('name', $user), 'Result has \'name\' field');
|
2019-04-03 16:43:32 +00:00
|
|
|
$this->assertEquals(true, ('' === $user['name'] || null === $user['name']), 'User name is empty string or null');
|
2019-04-03 16:19:03 +00:00
|
|
|
}
|
|
|
|
|
2019-04-03 16:38:57 +00:00
|
|
|
public function testAjaxUserAutoCompleteIsEmptyForNonExistingUser(): void
|
2016-12-12 00:22:02 +00:00
|
|
|
{
|
|
|
|
$client = static::createClient();
|
2016-12-12 00:27:35 +00:00
|
|
|
$client->request('GET', '/ajax/users/search/aksdjhaskdjhqwhdgqkjwhdgkjah');
|
2016-12-12 00:22:02 +00:00
|
|
|
|
|
|
|
$this->assertTrue($client->getResponse()->headers->contains('Content-Type', 'application/json'), 'Response has "Content-Type" = "application/json"');
|
|
|
|
|
|
|
|
$data = json_decode($client->getResponse()->getContent());
|
|
|
|
|
|
|
|
$this->assertNotNull($data, 'JSON data successfully decoded and not empty');
|
2019-04-03 16:38:57 +00:00
|
|
|
$this->assertInternalType('array', $data, 'JSON data is array');
|
|
|
|
$this->assertCount(0, $data, 'Array has no elements');
|
2016-12-12 00:22:02 +00:00
|
|
|
}
|
2019-04-03 16:19:03 +00:00
|
|
|
|
|
|
|
private function createClientForAjaxUserSearchByLogin(string $login): Client
|
|
|
|
{
|
|
|
|
$client = static::createClient();
|
|
|
|
$client->request('GET', '/ajax/users/search/'.$login);
|
|
|
|
|
|
|
|
return $client;
|
|
|
|
}
|
2015-05-30 06:22:06 +00:00
|
|
|
}
|