added controller to render markdown documentation #129
This commit is contained in:
74
tests/Controller/ControllerBaseTest.php
Normal file
74
tests/Controller/ControllerBaseTest.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Tests\Controller;
|
||||
|
||||
use App\DataFixtures\AppFixtures;
|
||||
use Symfony\Bundle\FrameworkBundle\Client;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
|
||||
/**
|
||||
* ControllerBaseTest adds some useful functions for writing integration tests.
|
||||
*/
|
||||
abstract class ControllerBaseTest extends WebTestCase
|
||||
{
|
||||
|
||||
const DEFAULT_LANGUAGE = 'en';
|
||||
|
||||
/**
|
||||
* @return Client
|
||||
*/
|
||||
protected function getClientForAuthenticatedUser()
|
||||
{
|
||||
$client = self::createClient([], [
|
||||
'PHP_AUTH_USER' => AppFixtures::USERNAME_USER,
|
||||
'PHP_AUTH_PW' => AppFixtures::DEFAULT_PASSWORD,
|
||||
]);
|
||||
|
||||
return $client;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Client $client
|
||||
* @param string $url
|
||||
* @param string $method
|
||||
* @return \Symfony\Component\DomCrawler\Crawler
|
||||
*/
|
||||
protected function request(Client $client, string $url, $method = 'GET')
|
||||
{
|
||||
return $client->request($method, '/' . self::DEFAULT_LANGUAGE . $url);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $url
|
||||
* @param string $method
|
||||
*/
|
||||
protected function assertUrlIsSecured(string $url, $method = 'GET')
|
||||
{
|
||||
$client = self::createClient();
|
||||
$client->request($method, '/' . self::DEFAULT_LANGUAGE . $url);
|
||||
|
||||
$this->assertTrue($client->getResponse()->isRedirect());
|
||||
|
||||
$this->assertEquals(
|
||||
'http://localhost/' . self::DEFAULT_LANGUAGE . '/login',
|
||||
$client->getResponse()->getTargetUrl(),
|
||||
sprintf('The %s secure URL redirects to the login form.', $url)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Client $client
|
||||
*/
|
||||
protected function assertRouteNotFound(Client $client)
|
||||
{
|
||||
$this->assertFalse($client->getResponse()->isSuccessful());
|
||||
$this->assertEquals(404, $client->getResponse()->getStatusCode());
|
||||
}
|
||||
}
|
||||
@@ -1,83 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Tests\Controller;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
|
||||
/**
|
||||
* TODO adjust to actual app
|
||||
*
|
||||
* Functional test that implements a "smoke test" of all the public and secure
|
||||
* URLs of the application.
|
||||
* See http://symfony.com/doc/current/best_practices/tests.html#functional-tests.
|
||||
*
|
||||
* Execute the application tests using this command (requires PHPUnit to be installed):
|
||||
*
|
||||
* $ cd your-symfony-project/
|
||||
* $ phpunit -c app
|
||||
*
|
||||
* @group integration
|
||||
*/
|
||||
class DefaultControllerTest extends WebTestCase
|
||||
{
|
||||
/**
|
||||
* PHPUnit's data providers allow to execute the same tests repeated times
|
||||
* using a different set of data each time.
|
||||
* See http://symfony.com/doc/current/cookbook/form/unit_testing.html#testing-against-different-sets-of-data.
|
||||
*
|
||||
* @dataProvider getPublicUrls
|
||||
*/
|
||||
public function testPublicUrls($url)
|
||||
{
|
||||
$this->markTestIncomplete(__METHOD__);
|
||||
$client = self::createClient();
|
||||
$client->request('GET', $url);
|
||||
|
||||
$this->assertTrue(
|
||||
$client->getResponse()->isSuccessful(),
|
||||
sprintf('The %s public URL loads correctly.', $url)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* The application contains a lot of secure URLs which shouldn't be
|
||||
* publicly accessible. This tests ensures that whenever a user tries to
|
||||
* access one of those pages, a redirection to the login form is performed.
|
||||
*
|
||||
* @dataProvider getSecureUrls
|
||||
*/
|
||||
public function testSecureUrls($url)
|
||||
{
|
||||
$this->markTestSkipped('No admin URLs for testing');
|
||||
$client = self::createClient();
|
||||
$client->request('GET', $url);
|
||||
|
||||
$this->assertTrue($client->getResponse()->isRedirect());
|
||||
|
||||
$this->assertEquals(
|
||||
'http://localhost/en/login',
|
||||
$client->getResponse()->getTargetUrl(),
|
||||
sprintf('The %s secure URL redirects to the login form.', $url)
|
||||
);
|
||||
}
|
||||
|
||||
public function getPublicUrls()
|
||||
{
|
||||
yield ['/'];
|
||||
yield ['/en/login'];
|
||||
}
|
||||
|
||||
public function getSecureUrls()
|
||||
{
|
||||
yield ['/en/admin/post/'];
|
||||
}
|
||||
}
|
||||
46
tests/Controller/HelpControllerTest.php
Normal file
46
tests/Controller/HelpControllerTest.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Tests\Controller;
|
||||
|
||||
/**
|
||||
* @group integration
|
||||
*/
|
||||
class HelpControllerTest extends ControllerBaseTest
|
||||
{
|
||||
|
||||
public function testIsSecure()
|
||||
{
|
||||
$this->assertUrlIsSecured('/help/');
|
||||
}
|
||||
|
||||
public function testReadmePage()
|
||||
{
|
||||
$client = $this->getClientForAuthenticatedUser();
|
||||
$this->request($client, '/help/');
|
||||
$this->assertTrue($client->getResponse()->isSuccessful());
|
||||
$this->assertContains('<h1>Kimai documentation</h1>', $client->getResponse()->getContent());
|
||||
$this->assertNotContains('<a href="/en/help/README">', $client->getResponse()->getContent());
|
||||
}
|
||||
|
||||
public function testUsersPage()
|
||||
{
|
||||
$client = $this->getClientForAuthenticatedUser();
|
||||
$this->request($client, '/help/users');
|
||||
$this->assertTrue($client->getResponse()->isSuccessful());
|
||||
$this->assertContains('<a href="/en/help/README">', $client->getResponse()->getContent());
|
||||
}
|
||||
|
||||
public function testValidateRouteDoesNotAllowSpecialChars()
|
||||
{
|
||||
$client = $this->getClientForAuthenticatedUser();
|
||||
$this->request($client, '/help/.users');
|
||||
$this->assertRouteNotFound($client);
|
||||
}
|
||||
}
|
||||
36
tests/Twig/MarkdownExtensionTest.php
Normal file
36
tests/Twig/MarkdownExtensionTest.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Tests\Twig;
|
||||
|
||||
use App\Twig\MarkdownExtension;
|
||||
use App\Utils\Markdown;
|
||||
use \PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @covers \App\Twig\MarkdownExtension
|
||||
*/
|
||||
class MarkdownExtensionTest extends TestCase
|
||||
{
|
||||
|
||||
public function testGetFilters()
|
||||
{
|
||||
$sut = new MarkdownExtension(new Markdown());
|
||||
$filters = $sut->getFilters();
|
||||
$this->assertCount(1, $filters);
|
||||
$this->assertEquals('md2html', $filters[0]->getName());
|
||||
}
|
||||
|
||||
public function testMarkdownToHtml()
|
||||
{
|
||||
$sut = new MarkdownExtension(new Markdown());
|
||||
$this->assertEquals('<p><em>test</em></p>', $sut->markdownToHtml('*test*'));
|
||||
$this->assertEquals('<h1>foobar</h1>', $sut->markdownToHtml('# foobar'));
|
||||
}
|
||||
}
|
||||
27
tests/Utils/MarkdownTest.php
Normal file
27
tests/Utils/MarkdownTest.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Tests\Utils;
|
||||
|
||||
use App\Utils\Markdown;
|
||||
use \PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @covers \App\Utils\Markdown
|
||||
*/
|
||||
class MarkdownTest extends TestCase
|
||||
{
|
||||
|
||||
public function testMarkdownToHtml()
|
||||
{
|
||||
$sut = new Markdown();
|
||||
$this->assertEquals('<p><em>test</em></p>', $sut->toHtml('*test*'));
|
||||
$this->assertEquals('<h1>foobar</h1>', $sut->toHtml('# foobar'));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user