예제 #1
0
 /**
  * Tests invalid (stale) form configurations.
  */
 function testInvalidForms()
 {
     $forms = ['nonexisting' => 'nonexisting_form', 'user' => 'user_nonexisting_form', 'node' => 'nonexisting_node_form', 'comment' => 'comment_node_nonexisting_form'];
     $mode = 0;
     foreach ($forms as $module => $form_id) {
         $mollom_form = FormController::getProtectedFormDetails($form_id, $module, []);
         $mollom_form['mode'] = $mode++;
         $form = Form::create($mollom_form);
         $form->id = $form_id;
         $form->save();
     }
     // Just visiting the form administration page is sufficient; it will throw
     // fatal errors, warnings, and notices.
     $this->drupalLogin($this->adminUser);
     $this->drupalGet('admin/config/content/mollom');
     // Ensure that unprotecting the forms does not throw any notices either.
     foreach ($forms as $form_id) {
         $this->assertNoLinkByHref('admin/config/content/mollom/form/' . $form_id . '/edit');
         $this->assertLinkByHref('admin/config/content/mollom/form/' . $form_id . '/delete');
         $this->drupalPostForm('admin/config/content/mollom/form/' . $form_id . '/delete', array(), t('Remove Mollom Protection'));
         $this->assertNoLinkByHref('admin/config/content/mollom/form/' . $form_id . '/delete');
     }
     // Confirm deletion.
     $configured = \Drupal::entityManager()->getStorage('mollom_form')->loadMultiple();
     $this->assertFalse($configured, 'No forms found.');
 }
예제 #2
0
 /**
  * Configure Mollom protection for a given form.
  *
  * @param $form_id
  *   The form id to configure.
  * @param $mode
  *   The Mollom protection mode for the form.
  * @param $fields
  *   (optional) A list of form elements to enable for text analysis. If
  *   omitted and the form registers individual elements, all fields are
  *   enabled by default.
  * @param $edit
  *   (optional) An array of POST data to pass through to drupalPost() when
  *   configuring the form's protection.
  */
 protected function setProtectionUI($form_id, $mode = FormInterface::MOLLOM_MODE_ANALYSIS, $fields = NULL, $edit = [])
 {
     // Always start from overview page, also to make debugging easier.
     $this->drupalGet('admin/config/content/mollom');
     // Determine whether the form is already protected.
     $exists = \Drupal::entityManager()->getStorage('mollom_form')->load($form_id);
     // Add a new form.
     if (!$exists) {
         $this->drupalGet('admin/config/content/mollom/add-form', ['query' => ['form_id' => $form_id]]);
         $save = t('Create Protected Mollom Form');
     } else {
         $this->assertLinkByHref('admin/config/content/mollom/form/' . $form_id . '/edit');
         $this->drupalGet('admin/config/content/mollom/form/' . $form_id . '/edit');
         $save = t('Update Protected Mollom Form');
     }
     $edit += ['mode' => $mode];
     // Process the enabled fields.
     $form_list = FormController::getProtectableForms();
     $form_info = FormController::getProtectedFormDetails($form_id, $form_list[$form_id]['module']);
     if (!empty($form_info['elements'])) {
         $edit += ['checks[spam]' => TRUE];
     }
     foreach (array_keys($form_info['elements']) as $field) {
         if (!isset($fields) || in_array($field, $fields)) {
             // If the user specified all fields by default or to include this
             // field, set its checkbox value to TRUE.
             $edit['enabled_fields[' . rawurlencode($field) . ']'] = TRUE;
         } else {
             // Otherwise set the field's checkbox value to FALSE.
             $edit['enabled_fields[' . rawurlencode($field) . ']'] = FALSE;
         }
     }
     $this->drupalPostForm(NULL, $edit, $save);
     if (!$exists) {
         $this->assertText(t('The form protection has been added.'));
     } else {
         $this->assertText(t('The form protection has been updated.'));
     }
 }
예제 #3
0
 /**
  * {@inheritDoc}
  *
  */
 public function initialize($form_id = NULL)
 {
     $mollom_form = get_object_vars($this);
     if (empty($form_id) && empty($this->id)) {
         return $mollom_form;
     }
     if ($this->isNew()) {
         $forms = FormController::getProtectableForms();
         if (empty($forms[$form_id])) {
             return $mollom_form;
         }
         $mollom_form += $forms[$form_id];
         $this->id = $form_id;
         $this->label = $forms[$form_id]['title'];
         foreach ($forms[$form_id] as $name => $value) {
             if (property_exists($this, $name)) {
                 $this->{$name} = $value;
             }
         }
         $module = $this->module;
     } else {
         $form_id = $this->id();
         $module = $this->module;
         $forms = NULL;
     }
     // Add all of the configuration information defined in hooks.
     $form_details = FormController::getProtectedFormDetails($form_id, $module, $forms);
     if ($this->isNew()) {
         // Overwrite the element properties with form details when supplied.
         $mollom_form = array_merge($mollom_form, $form_details);
     } else {
         // The entity has already been configured so use it's data over the
         // configuration details.
         $mollom_form = array_merge($form_details, $mollom_form);
     }
     if ($this->isNew()) {
         // Enable all fields for textual analysis by default.
         $this->setChecks(array('spam'));
         $mollom_form['checks'] = array('spam');
         $mollom_form['enabled_fields'] = array_keys($mollom_form['elements']);
         $this->setEnabledFields(array_keys($mollom_form['elements']));
     }
     return $mollom_form;
 }