Skip to content
This repository has been archived by the owner on Aug 20, 2018. It is now read-only.

Commit

Permalink
TPresenter: add checkAjaxForm helper
Browse files Browse the repository at this point in the history
  • Loading branch information
mrtnzlml committed Jan 23, 2016
1 parent c183472 commit ccc965c
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 2 deletions.
27 changes: 26 additions & 1 deletion src/traits/TPresenter.php
Expand Up @@ -16,6 +16,8 @@ trait TPresenter

private $exception;

private $ajaxMode = FALSE;

/**
* @param string $destination
* @param array $params
Expand All @@ -34,11 +36,13 @@ public function check($destination, $params = [], $post = [])
if (!$this->presenter) {
$container = $this->getContainer();
$container->removeService('httpRequest');
$container->addService('httpRequest', new HttpRequestMock);
$headers = $this->ajaxMode ? ['X-Requested-With' => 'XMLHttpRequest'] : [];
$container->addService('httpRequest', new HttpRequestMock(NULL, NULL, [], [], [], $headers));
$presenterFactory = $container->getByType('Nette\Application\IPresenterFactory');
$class = $presenterFactory->getPresenterClass($presenter);
$this->presenter = $container->createInstance($class);
$this->presenter->autoCanonicalize = FALSE;
$this->presenter->invalidLinkMode = \Nette\Application\UI\Presenter::INVALID_LINK_EXCEPTION;
$container->callInjects($this->presenter);
}
$request = new ApplicationRequestMock(
Expand Down Expand Up @@ -170,6 +174,27 @@ public function checkForm($destination, $formName, $post = [], $path = '/')
}
}

public function checkAjaxForm($destination, $formName, $post = [], $path = FALSE)
{
if (is_string($path)) {
$this->checkForm($destination, $formName, $post, $path);
Assert::false($this->presenter->isAjax());
}
$this->presenter = NULL; //FIXME: not very nice, but performance first
$this->ajaxMode = TRUE;
$response = $this->check($destination, [
'do' => $formName . '-submit',
], $post);
Assert::true($this->presenter->isAjax());
if (!$this->exception) {
Assert::same(200, $this->getReturnCode());
Assert::type('Nette\Application\Responses\JsonResponse', $response);
}
$this->presenter = NULL;
$this->ajaxMode = FALSE;
return $response;
}

/**
* @param string $destination
* @param array $params
Expand Down
20 changes: 20 additions & 0 deletions tests/Traits/TPresenterTest.phpt
Expand Up @@ -29,6 +29,11 @@ class TPresenterTest extends \Tester\TestCase
$this->checkAction(':Presenter:default');
}

public function testRenderBrokenLink()
{
$this->checkAction('Presenter:brokenLink'); //FIXME: should fail (?)
}

public function test404Render()
{
Assert::exception(function () {
Expand Down Expand Up @@ -143,6 +148,21 @@ class TPresenterTest extends \Tester\TestCase
], FALSE); //do not check redirect
}

public function testAjaxForm()
{
$this->checkForm('Presenter:default', 'ajaxForm', [
'test' => 'test',
], '/x/y/json');

$this->checkAjaxForm('Presenter:default', 'ajaxForm', [
'test' => 'test',
]);

$this->checkAjaxForm('Presenter:default', 'ajaxForm', [
'test' => 'test',
], '/x/y/json');
}

public function testSignal()
{
$this->checkSignal('Presenter:default', 'signal');
Expand Down
15 changes: 15 additions & 0 deletions tests/_helpers/presenters/PresenterPresenter.php
Expand Up @@ -86,6 +86,21 @@ protected function createComponentForm3()
return $form;
}

protected function createComponentAjaxForm()
{
$form = new \Nette\Application\UI\Form();
$form->addText('test');
$form->onSuccess[] = function ($_, $values) {
$this->flashMessage(json_encode($values));
if ($this->isAjax()) {
$this->redrawControl();
} else {
$this->redirect('json');
}
};
return $form;
}

public function handleSignal()
{
$this->flashMessage('OK');
Expand Down
12 changes: 12 additions & 0 deletions tests/_helpers/presenters/templates/Presenter/brokenLink.latte
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>TEST</title>
</head>
<body>
<div>
<a n:href="wrongdestination">BROKEN LINK</a>
</div>
</body>
</html>
Expand Up @@ -3,6 +3,6 @@

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url n:foreach="$sitemap as $s">
<loc>{link //Homepage:default}</loc>
<loc>{link //Presenter:default}</loc>
</url>
</urlset>

0 comments on commit ccc965c

Please sign in to comment.