From e2b98a41381eec9fad778793a86991cb2b02f167 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jannis=20Mo=C3=9Fhammer?= <jannis.mosshammer@netways.de>
Date: Fri, 26 Jul 2013 10:34:45 +0200
Subject: [PATCH] Fix broken login form

The login form was broken, as it used the old form api (disableCSRFToken
instead of setTokenDisabled and isValid instead of isPostAndValid)

refs #4459
---
 .../controllers/AuthenticationController.php  |  2 +-
 .../forms/Authentication/LoginForm.php        |  2 +-
 .../regression/LoginMaskBroken4459Test.php    | 64 +++++++++++++++++++
 3 files changed, 66 insertions(+), 2 deletions(-)
 create mode 100644 test/php/regression/LoginMaskBroken4459Test.php

diff --git a/application/controllers/AuthenticationController.php b/application/controllers/AuthenticationController.php
index e66bf3f95..e1622e947 100644
--- a/application/controllers/AuthenticationController.php
+++ b/application/controllers/AuthenticationController.php
@@ -69,7 +69,7 @@ class AuthenticationController extends ActionController
                 $this->redirectNow('index?_render=body');
             }
 
-            if ($this->getRequest()->isPost() && $this->view->form->isValid($this->getRequest())) {
+            if ($this->view->form->isPostAndValid()) {
 
 
                 $credentials->setUsername($this->view->form->getValue('username'));
diff --git a/application/forms/Authentication/LoginForm.php b/application/forms/Authentication/LoginForm.php
index 242ae4a71..30aff96ad 100644
--- a/application/forms/Authentication/LoginForm.php
+++ b/application/forms/Authentication/LoginForm.php
@@ -64,6 +64,6 @@ class LoginForm extends Form
             )
         );
 
-        $this->disableCsrfToken();
+        $this->setTokenDisabled(true);
     }
 }
diff --git a/test/php/regression/LoginMaskBroken4459Test.php b/test/php/regression/LoginMaskBroken4459Test.php
new file mode 100644
index 000000000..fee3736cd
--- /dev/null
+++ b/test/php/regression/LoginMaskBroken4459Test.php
@@ -0,0 +1,64 @@
+<?php
+namespace {
+    if (!function_exists('t')) {
+        function t() {
+            return func_get_arg(0);
+        }
+    }
+
+    if (!function_exists('mt')) {
+        function mt() {
+            return func_get_arg(0);
+        }
+    }
+}
+
+namespace Tests\Icinga\Regression
+{
+
+    use Icinga\Form\Authentication\LoginForm;
+
+    require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';
+    require_once 'Zend/Form.php';
+    require_once 'Zend/View.php';
+    require_once 'Zend/Form/Element/Submit.php';
+    require_once 'Zend/Form/Element/Reset.php';
+    require_once 'Zend/Form/Element/Checkbox.php';
+    require_once 'Zend/Form/Element/Hidden.php';
+    require_once 'Zend/Validate/Date.php';
+    require_once '../../library/Icinga/Web/Form.php';
+    require_once realpath('../../application/forms/Authentication/LoginForm.php');
+
+
+    class LoginMaskBrokenRegression_4459Test extends \Zend_Test_PHPUnit_ControllerTestCase
+    {
+
+        public function testShowLoginForm()
+        {
+            $view = new \Zend_View();
+            $form = new LoginForm();
+            $form->buildForm();
+            $rendered = $form->render($view);
+
+            $this->assertContains("<form", $rendered, "Asserting a form being returned when displaying the login form");
+        }
+
+        public function testSubmitLoginForm()
+        {
+            $request = $this->getRequest();
+
+            $request->setMethod("POST")->setPost(array(
+                "username" => "test",
+                "password" => "test"
+            ));
+
+            $view = new \Zend_View();
+            $form = new LoginForm();
+            $form->setRequest($request);
+            $form->buildForm();
+            $this->assertTrue($form->isPostAndValid());
+
+        }
+    }
+
+}
\ No newline at end of file