Skip to content

Commit

Permalink
Added tests from aptoma
Browse files Browse the repository at this point in the history
Needs to be modified for our new directories
  • Loading branch information
jason490 committed Nov 6, 2023
1 parent 39b73aa commit f31c00e
Show file tree
Hide file tree
Showing 6 changed files with 335 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Test

on: [push, pull_request]

jobs:
run:
runs-on: 'ubuntu-latest'
strategy:
matrix:
php-versions: ['7.2', '7.3', '7.4', '8.0']

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
uses: ramsey/composer-install@v1

- name: Run Tests
run: vendor/bin/phpunit
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Aptoma\Twig\Extension\MarkdownEngine;

use Aptoma\Twig\Extension\MarkdownExtensionTest;
use Github\Client;

require_once(__DIR__ . '/../MarkdownExtensionTest.php');

/**
* Class GitHubMarkdownEngineTest
*
* @author Lukas W <[email protected]>
*/
class GitHubMarkdownEngineTest extends MarkdownExtensionTest
{
/**
* @dataProvider getParseMarkdownTests
*/
public function testParseMarkdown($template, $expected, $context = array())
{
try {
$this->assertEquals($expected, $this->getTemplate($template)->render($context));
} catch (\Exception $e) {
$this->markTestSkipped($e->getMessage());
}
}

public function getParseMarkdownTests()
{
return array(
array('{{ "# Main Title"|markdown }}', '<h1>Main Title</h1>'),
array('{{ content|markdown }}', '<h1>Main Title</h1>', array('content' => '# Main Title')),
// Check if GFM is working
array('{{ "@aptoma"|markdown }}',
'<p><a href="https://github.com/aptoma" class="user-mention">@aptoma</a></p>'),
);
}

protected function getEngine()
{
$client = new Client();

if ($client->rateLimit()->getResource('core')->getLimit() < 1) {
$this->markTestSkipped('The github API rate limit is reached, so this engine cannot be tested.');
}

return new GitHubMarkdownEngine();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Aptoma\Twig\Extension\MarkdownEngine;

use Aptoma\Twig\Extension\MarkdownExtensionTest;

// Require parent class if not autoloaded
if (!class_exists('\Aptoma\Twig\Extension\MarkdownExtensionTest')) {
require_once(__DIR__ . '/../MarkdownExtensionTest.php');
}

/**
* Class PHPLeagueCommonMarkEngineTest
*
* @author Casey McLaughlin <[email protected]>
*/
class PHPLeagueCommonMarkEngineTest extends MarkdownExtensionTest
{
protected function getEngine()
{
return new PHPLeagueCommonMarkEngine();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Aptoma\Twig\Extension\MarkdownEngine;

use Aptoma\Twig\Extension\MarkdownExtension;
use Aptoma\Twig\Extension\MarkdownExtensionTest;

// Require parent class if not autoloaded
if (!class_exists('\Aptoma\Twig\Extension\MarkdownExtensionTest')) {
require_once(__DIR__ . '/../MarkdownExtensionTest.php');
}

/**
* Class ParsedownEngineTest
*
* @author Sébastien Lourseau <https://github.com/SebLours>
*/
class ParsedownEngineTest extends MarkdownExtensionTest
{
public function getParseMarkdownTests()
{
return array(
array('{{ "# Main Title"|markdown }}', '<h1>Main Title</h1>'),
array('{{ content|markdown }}', '<h1>Main Title</h1>', array('content' => '# Main Title')),
array('{% markdown %}{{ content }}{% endmarkdown %}', '<h1>Main Title</h1>', array('content' => '# Main Title'))
);
}

protected function getEngine()
{
return new ParsedownEngine();
}

public function testSafeMode()
{
$engine = $this->getEngine();
$loader = new \Twig\Loader\ArrayLoader(array('index' => '{{ "_Test_<em>Test</em>[xss](javascript:alert%281%29)"|markdown }}'));
$twig = new \Twig\Environment($loader, array('debug' => true, 'cache' => false));
$twig->addExtension(new MarkdownExtension($engine));

$this->assertEquals('<p><em>Test</em><em>Test</em><a href="javascript:alert%281%29">xss</a></p>', $twig->load('index')->render());

$engine->setSafeMode(true);
$this->assertEquals('<p><em>Test</em>&lt;em&gt;Test&lt;/em&gt;<a href="javascript%3Aalert%281%29">xss</a></p>', $twig->load('index')->render());
$engine->setSafeMode(false);
}

public function testMarkupEscape()
{
$engine = $this->getEngine();
$loader = new \Twig\Loader\ArrayLoader(array('index' => '{{ "_Test_<em>Test</em>[xss](javascript:alert%281%29)"|markdown }}'));
$twig = new \Twig\Environment($loader, array('debug' => true, 'cache' => false));
$twig->addExtension(new MarkdownExtension($engine));

$this->assertEquals('<p><em>Test</em><em>Test</em><a href="javascript:alert%281%29">xss</a></p>', $twig->load('index')->render());

$engine->setMarkupEscaped(true);
$this->assertEquals('<p><em>Test</em>&lt;em&gt;Test&lt;/em&gt;<a href="javascript:alert%281%29">xss</a></p>', $twig->load('index')->render());
$engine->setMarkupEscaped(false);
}
}
42 changes: 42 additions & 0 deletions tests/Submitty/Twig/Extension/MarkdownExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Aptoma\Twig\Extension;

use Aptoma\Twig\Extension\MarkdownEngine\MichelfMarkdownEngine;
use PHPUnit\Framework\TestCase;

/**
* @author Gunnar Liun <[email protected]>
*/
class MarkdownExtensionTest extends TestCase
{
/**
* @dataProvider getParseMarkdownTests
*/
public function testParseMarkdown($template, $expected, $context = array())
{
$this->assertEquals($expected, $this->getTemplate($template)->render($context));
}

public function getParseMarkdownTests()
{
return array(
array('{{ "# Main Title"|markdown }}', '<h1>Main Title</h1>' . PHP_EOL),
array('{{ content|markdown }}', '<h1>Main Title</h1>' . PHP_EOL, array('content' => '# Main Title'))
);
}

protected function getEngine()
{
return new MichelfMarkdownEngine();
}

protected function getTemplate($template)
{
$loader = new \Twig\Loader\ArrayLoader(array('index' => $template));
$twig = new \Twig\Environment($loader, array('debug' => true, 'cache' => false));
$twig->addExtension(new MarkdownExtension($this->getEngine()));

return $twig->load('index');
}
}
137 changes: 137 additions & 0 deletions tests/Submitty/Twig/TokenParser/MarkdownTokenParserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

namespace Aptoma\Twig\TokenParser;

use Aptoma\Twig\Extension\MarkdownEngine\MichelfMarkdownEngine;
use Aptoma\Twig\Node\MarkdownNode;
use PHPUnit\Framework\TestCase;
use Twig\Compiler;
use Twig\Environment;
use Twig\Loader\ArrayLoader;
use Twig\Node\Node;
use Twig\Node\TextNode;

/**
* @author Gunnar Lium <[email protected]>
*/
class MarkdownTokenParserTest extends TestCase
{
public function testConstructor()
{
$body = new Node(array(new TextNode("#Title\n\nparagraph\n", 1)));
$node = new MarkdownNode($body, 1);

$this->assertEquals($body, $node->getNode('body'));
}

/**
* Test that the generated code actually do what we expect
*
* The contents of this test is the same that we write in the compile method.
* This requires manual synchronization, which we should probably not rely on.
*/
public function testMarkdownPrepareBehavior()
{
$body = " #Title\n\n paragraph\n\n code";
$bodyPrepared = "#Title\n\nparagraph\n\n code";

ob_start();
echo $body;
$content = ob_get_clean();
preg_match("/^\s*/", $content, $matches);
$lines = explode("\n", $content);
$content = preg_replace('/^' . $matches[0]. '/', "", $lines);
$content = join("\n", $content);

// Assert prepared content looks right
$this->assertEquals($bodyPrepared, $content);

// Assert Markdown output
$expectedOutput = "<h1>Title</h1>\n\n<p>paragraph</p>\n\n<pre><code>code\n</code></pre>\n";
$this->assertEquals($expectedOutput, $this->getEngine()->transform($content));
}

/**
* Test that the generated code looks as expected
*
* @dataProvider getTests
*/
public function testCompile($node, $source, $environment = null, $isPattern = false)
{
$this->assertNodeCompilation($source, $node, $environment, $isPattern = false);
}

protected function getEngine()
{
return new MichelfMarkdownEngine();
}

public function getTests()
{
$tests = array();

$body = new Node(array(new TextNode("#Title\n\nparagraph\n", 1)));
$node = new MarkdownNode($body, 1);

$tests['simple text'] = array($node, <<<EOF
// line 1
ob_start();
echo "#Title
paragraph
";
\$content = ob_get_clean();
preg_match("/^\s*/", \$content, \$matches);
\$lines = explode("\\n", \$content);
\$content = preg_replace('/^' . \$matches[0]. '/', "", \$lines);
\$content = join("\\n", \$content);
echo \$this->env->getExtension('Aptoma\Twig\Extension\MarkdownExtension')->parseMarkdown(\$content);
EOF
);

$body = new Node(array(new TextNode(" #Title\n\n paragraph\n\n code\n", 1)));
$node = new MarkdownNode($body, 1);

$tests['text with leading indent'] = array($node, <<<EOF
// line 1
ob_start();
echo " #Title
paragraph
code
";
\$content = ob_get_clean();
preg_match("/^\s*/", \$content, \$matches);
\$lines = explode("\\n", \$content);
\$content = preg_replace('/^' . \$matches[0]. '/', "", \$lines);
\$content = join("\\n", \$content);
echo \$this->env->getExtension('Aptoma\Twig\Extension\MarkdownExtension')->parseMarkdown(\$content);
EOF
);

return $tests;
}

public function assertNodeCompilation($source, Node $node, Environment $environment = null, $isPattern = false)
{
$compiler = $this->getCompiler($environment);
$compiler->compile($node);

if ($isPattern) {
$this->assertStringMatchesFormat($source, trim($compiler->getSource()));
} else {
$this->assertEquals($source, trim($compiler->getSource()));
}
}

protected function getCompiler(Environment $environment = null)
{
return new Compiler(null === $environment ? $this->getEnvironment() : $environment);
}

protected function getEnvironment()
{
return new Environment(new ArrayLoader(array()));
}
}

0 comments on commit f31c00e

Please sign in to comment.