From cfc30aadc845b7c74e2222a27a5720a1eb509ff0 Mon Sep 17 00:00:00 2001
From: bhavik shah <bhavikshah835@gmail.com>
Date: Thu, 15 Dec 2016 20:34:56 +0530
Subject: [PATCH] Configuration validation automated test-Issue#2817181

---
 form_error_logging.test | 134 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 134 insertions(+)

diff --git a/form_error_logging.test b/form_error_logging.test
index bf1196b..71ce73e 100644
--- a/form_error_logging.test
+++ b/form_error_logging.test
@@ -45,3 +45,137 @@ class FormErrorLoggingHelpTestCase extends DrupalWebTestCase {
     );
   }
 }
+
+/**
+ * Configuration validation test case.
+ */
+class FormErrorLoggingConfigTestCase extends DrupalWebTestCase {
+
+  /**
+   * Implements getInfo().
+   */
+  public static function getInfo() {
+    return array(
+      'name' => t('Configuration validation'),
+      'description' => t('Configuration validation tests for Form Logging Error.'),
+      'group' => 'Form Logging Error',
+    );
+  }
+
+  /**
+   * Implements setUp().
+   */
+  public function setUp() {
+    // Enable any module that you will need in your tests.
+    parent::setUp('form_error_logging', 'contact');
+
+    // Valid user with appropriate permissions.
+    $this->valid_user = $this->drupalCreateUser(
+      array(
+        'access administration pages',
+        'administer site configuration',
+        'access site-wide contact form'
+      )
+    );
+  }
+
+  /**
+   * This is helper function.
+   * It checks whether User has permissions to access module's config form.
+   * And add new form ID whose errors has to be logged-in
+   */
+  private function beginFormLogging() {
+    // check whether module's config form is accessible
+    $this->drupalGet('admin/config/development/logging/form-error-logging');
+    $this->assertResponse(200, "Form is accessible");
+
+    // check whether Textarea is available
+    $this->assertFieldByName('form_error_logging_forms', '', "Textarea is present");
+
+    // Add Contact form to be tracked
+    $data = array('form_error_logging_forms' => 'contact_site_form');
+    $this->drupalPost('admin/config/development/logging/form-error-logging', $data, t('Save configuration'));
+    $this->assertText(t('The configuration options have been saved.'), "Form saved");
+  }
+
+  /**
+   * This is helper function.
+   * It checks whether User has permissions to access Contact form.
+   * And submits the form to generate error.
+   *
+   * @param $mode
+   *   The Assertion mode.
+   */
+  private function submitLoggedForm($mode) {
+    // check whether contact form is accessible
+    $this->drupalGet('contact');
+    $this->assertResponse(200, "Contact Form is accessible");
+
+    // check whether Name field is available
+    $this->assertFieldByName('name', '', "Name textbox is present");
+
+    // Submit Contact form
+    $data = array('name' => '');
+    $this->drupalPost('contact', $data, t('Send message'));
+    $this->assertWatchdogMessage(
+      'Form error was raised for "@element" with a message of "@message"',
+      array(
+        '@element' => 'name',
+        '@message' => 'Your name field is required.'
+      ),
+      'Found watchdog message',
+      $mode
+    );
+  }
+
+  /**
+   * Valid User Login and visit config page, assert HTML status code.
+   */
+  public function testValidForm() {
+    $this->drupalLogin($this->valid_user);
+    $this->beginFormLogging();
+    $this->submitLoggedForm(TRUE);
+    $this->drupalLogout();
+  }
+
+  /**
+   * Valid User Login and visit config page, assert HTML status code.
+   */
+  public function testInvalidForm() {
+    $this->drupalLogin($this->valid_user);
+    $this->submitLoggedForm(FALSE);
+    $this->drupalLogout();
+  }
+
+  /**
+   * This is helper function.
+   *
+   * Verify that a watchdog message has been entered.
+   *
+   * @param $watchdog_message
+   *   The watchdog message.
+   * @param $variables
+   *   The array of variables passed to watchdog().
+   * @param $message
+   *   The assertion message.
+   * @param $mode
+   *   The Assertion mode.
+   */
+  function assertWatchdogMessage($watchdog_message, $variables, $message, $mode) {
+    $status = (bool) db_query_range(
+      "SELECT 1 FROM {watchdog} WHERE message = :message AND variables = :variables",
+      0,
+      1,
+      array(
+        ':message' => $watchdog_message,
+        ':variables' => serialize($variables)
+      )
+    )->fetchField();
+    if ($mode) {
+      return $this->assertTrue($status, format_string('@message', array('@message' => $message)));
+    }
+    else {
+      return $this->assertFalse($status, format_string('@message', array('@message' => $message)));
+    }
+  }
+}
-- 
1.9.5.msysgit.1

