submitContact example

// Check to see that anonymous user cannot see contact page without permission.     user_role_revoke_permissions(RoleInterface::ANONYMOUS_ID, ['access site-wide contact form']);
    $this->drupalGet('contact');
    $this->assertSession()->statusCodeEquals(403);

    // Give anonymous user permission and see that page is viewable.     user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['access site-wide contact form']);
    $this->drupalGet('contact');
    $this->assertSession()->statusCodeEquals(200);

    // Submit contact form with invalid values.     $this->submitContact('', $recipients[0]$this->randomMachineName(16)$id$this->randomMachineName(64));
    $this->assertSession()->pageTextContains('Your name field is required.');

    $this->submitContact($this->randomMachineName(16), '', $this->randomMachineName(16)$id$this->randomMachineName(64));
    $this->assertSession()->pageTextContains('Your email address field is required.');

    $this->submitContact($this->randomMachineName(16)$invalid_recipients[0]$this->randomMachineName(16)$id$this->randomMachineName(64));
    $this->assertSession()->pageTextContains('The email address invalid is not valid.');

    $this->submitContact($this->randomMachineName(16)$recipients[0], '', $id$this->randomMachineName(64));
    $this->assertSession()->pageTextContains('Subject field is required.');

    
$this->addContactForm($id = mb_strtolower($this->randomMachineName(16))$label = $this->randomMachineName(16)implode(',', [$mail]), '', TRUE, 'Your message has been sent.', [
      'send_a_pony' => 1,
    ]);
    $this->assertSession()->pageTextContains('Contact form ' . $label . ' has been added.');

    // Ensure that anonymous can submit site-wide contact form.     user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['access site-wide contact form']);
    $this->drupalLogout();
    $this->drupalGet('contact');
    $this->assertSession()->pageTextContains('Your email address');
    $this->assertSession()->pageTextNotContains('Form');
    $this->submitContact($name = $this->randomMachineName(16)$mail$subject = $this->randomMachineName(16)$id$message = $this->randomMachineName(64));
    $this->assertSession()->pageTextContains('Your message has been sent.');

    $messages = Message::loadMultiple();
    /** @var \Drupal\contact\Entity\Message $message */
    $message = reset($messages);
    $this->assertEquals($id$message->getContactForm()->id());
    $this->assertTrue($message->getContactForm()->getThirdPartySetting('contact_storage_test', 'send_a_pony', FALSE));
    $this->assertEquals($name$message->getSenderName());
    $this->assertEquals($subject$message->getSubject());
    $this->assertEquals($mail$message->getSenderMail());

    
Home | Imprint | This part of the site doesn't use cookies.