summaryrefslogtreecommitdiff
path: root/modules/akismet
diff options
context:
space:
mode:
Diffstat (limited to 'modules/akismet')
-rw-r--r--modules/akismet/controllers/admin_akismet.php11
-rw-r--r--modules/akismet/helpers/akismet.php17
-rw-r--r--modules/akismet/tests/Akismet_Helper_Test.php45
3 files changed, 38 insertions, 35 deletions
diff --git a/modules/akismet/controllers/admin_akismet.php b/modules/akismet/controllers/admin_akismet.php
index ca3a1473..4847db53 100644
--- a/modules/akismet/controllers/admin_akismet.php
+++ b/modules/akismet/controllers/admin_akismet.php
@@ -25,17 +25,8 @@ class Admin_Akismet_Controller extends Admin_Controller {
// @todo move the "post" handler part of this code into a separate function
access::verify_csrf();
- $valid = $form->validate();
-
- if ($valid) {
+ if ($form->validate()) {
$new_key = $form->configure_akismet->api_key->value;
- if ($new_key && !akismet::validate_key($new_key)) {
- $form->configure_akismet->api_key->add_error("invalid", 1);
- $valid = false;
- }
- }
-
- if ($valid) {
$old_key = module::get_var("akismet", "api_key");
if ($old_key && !$new_key) {
message::success(t("Your Akismet key has been cleared."));
diff --git a/modules/akismet/helpers/akismet.php b/modules/akismet/helpers/akismet.php
index 46a305b2..b4405de5 100644
--- a/modules/akismet/helpers/akismet.php
+++ b/modules/akismet/helpers/akismet.php
@@ -23,8 +23,9 @@ class akismet_Core {
static function get_configure_form() {
$form = new Forge("admin/akismet", "", "post", array("id" => "g-configure-akismet-form"));
$group = $form->group("configure_akismet")->label(t("Configure Akismet"));
- $group->input("api_key")->label(t("API Key"))->value(module::get_var("akismet", "api_key"));
- $group->api_key->error_messages("invalid", t("The API key you provided is invalid."));
+ $group->input("api_key")->label(t("API Key"))->value(module::get_var("akismet", "api_key"))
+ ->callback("akismet::validate_key")
+ ->error_messages("invalid", t("The API key you provided is invalid."));
$group->submit("")->value(t("Save"));
return $form;
}
@@ -82,10 +83,14 @@ class akismet_Core {
* @param string $api_key the API key
* @return boolean
*/
- static function validate_key($api_key) {
- $request = self::_build_verify_request($api_key);
- $response = self::_http_post($request, "rest.akismet.com");
- return "valid" == $response->body[0];
+ static function validate_key($api_key_input) {
+ if ($api_key_input->value) {
+ $request = self::_build_verify_request($api_key_input->value);
+ $response = self::_http_post($request, "rest.akismet.com");
+ if ("valid" != $response->body[0]) {
+ $api_key_input->add_error("invalid", 1);
+ }
+ }
}
diff --git a/modules/akismet/tests/Akismet_Helper_Test.php b/modules/akismet/tests/Akismet_Helper_Test.php
index d8605d5c..e185f280 100644
--- a/modules/akismet/tests/Akismet_Helper_Test.php
+++ b/modules/akismet/tests/Akismet_Helper_Test.php
@@ -17,25 +17,32 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
-class Akismet_Helper_Test extends Unit_Test_Case {
+class Akismet_Helper_Test extends Gallery_Unit_Test_Case {
private $_comment;
public function setup() {
Input::instance()->ip_address = "1.1.1.1";
request::set_user_agent("Akismet_Helper_Test");
+ module::set_var("akismet", "api_key", "TEST_KEY");
+ }
+
+ private function _make_comment() {
+ $comment = ORM::factory("comment");
+ $comment->item_id = item::root()->id;
+ $comment->author_id = identity::guest()->id;
+ $comment->text = "This is a comment";
+ $comment->guest_name = "John Doe";
+ $comment->guest_email = "john@gallery2.org";
+ $comment->guest_url = "http://gallery2.org";
+ $comment->save();
- $root = ORM::factory("item", 1);
- $this->_comment = comment::create(
- $root, identity::guest(), "This is a comment",
- "John Doe", "john@gallery2.org", "http://gallery2.org");
- foreach ($this->_comment->list_fields("comments") as $name => $field) {
+ // Set the server fields to a known placeholder
+ foreach ($comment->list_fields("comments") as $name => $field) {
if (strpos($name, "server_") === 0) {
- $this->_comment->$name = substr($name, strlen("server_"));
+ $comment->$name = substr($name, strlen("server_"));
}
}
- $this->_comment->save();
-
- module::set_var("akismet", "api_key", "TEST_KEY");
+ return $comment->save();
}
public function build_verify_request_test() {
@@ -51,8 +58,8 @@ class Akismet_Helper_Test extends Unit_Test_Case {
}
public function build_comment_check_request_test() {
- $request = akismet::_build_request("comment-check", $this->_comment);
- $id = $this->_comment->id;
+ $comment = $this->_make_comment();
+ $request = akismet::_build_request("comment-check", $comment);
$expected = "POST /1.1/comment-check HTTP/1.0\r\n" .
"Host: TEST_KEY.rest.akismet.com\r\n" .
"Content-Type: application/x-www-form-urlencoded; charset=UTF-8\r\n" .
@@ -66,15 +73,15 @@ class Akismet_Helper_Test extends Unit_Test_Case {
"SERVER_HTTP_ACCEPT_CHARSET=http_accept_charset&" .
"blog=http%3A%2F%2F.%2F&comment_author=John+Doe&comment_author_email=john%40gallery2.org&" .
"comment_author_url=http%3A%2F%2Fgallery2.org&comment_content=This+is+a+comment&" .
- "comment_type=comment&permalink=http%3A%2F%2F.%2Findex.php%2Fcomments%2F{$id}&" .
+ "comment_type=comment&permalink=http%3A%2F%2F.%2Findex.php%2Fcomments%2F{$comment->id}&" .
"referrer=http_referer&user_agent=http_user_agent&user_ip=remote_addr";
$this->assert_equal($expected, $request);
}
public function build_submit_spam_request_test() {
- $request = akismet::_build_request("submit-spam", $this->_comment);
- $id = $this->_comment->id;
+ $comment = $this->_make_comment();
+ $request = akismet::_build_request("submit-spam", $comment);
$expected =
"POST /1.1/submit-spam HTTP/1.0\r\n" .
"Host: TEST_KEY.rest.akismet.com\r\n" .
@@ -89,15 +96,15 @@ class Akismet_Helper_Test extends Unit_Test_Case {
"SERVER_HTTP_ACCEPT_CHARSET=http_accept_charset&" .
"blog=http%3A%2F%2F.%2F&comment_author=John+Doe&comment_author_email=john%40gallery2.org&" .
"comment_author_url=http%3A%2F%2Fgallery2.org&comment_content=This+is+a+comment&" .
- "comment_type=comment&permalink=http%3A%2F%2F.%2Findex.php%2Fcomments%2F{$id}&" .
+ "comment_type=comment&permalink=http%3A%2F%2F.%2Findex.php%2Fcomments%2F{$comment->id}&" .
"referrer=http_referer&user_agent=http_user_agent&user_ip=remote_addr";
$this->assert_equal($expected, $request);
}
public function build_submit_ham_request_test() {
- $request = akismet::_build_request("submit-ham", $this->_comment);
- $id = $this->_comment->id;
+ $comment = $this->_make_comment();
+ $request = akismet::_build_request("submit-ham", $comment);
$expected =
"POST /1.1/submit-ham HTTP/1.0\r\n" .
"Host: TEST_KEY.rest.akismet.com\r\n" .
@@ -112,7 +119,7 @@ class Akismet_Helper_Test extends Unit_Test_Case {
"SERVER_HTTP_ACCEPT_CHARSET=http_accept_charset&blog=http%3A%2F%2F.%2F&" .
"comment_author=John+Doe&comment_author_email=john%40gallery2.org&" .
"comment_author_url=http%3A%2F%2Fgallery2.org&comment_content=This+is+a+comment&" .
- "comment_type=comment&permalink=http%3A%2F%2F.%2Findex.php%2Fcomments%2F{$id}&" .
+ "comment_type=comment&permalink=http%3A%2F%2F.%2Findex.php%2Fcomments%2F{$comment->id}&" .
"referrer=http_referer&user_agent=http_user_agent&user_ip=remote_addr";
$this->assert_equal($expected, $request);