public function testRenderForm() { $app = $this->getApp(); $app['request'] = Request::create('/'); $boltforms = new BoltForms($app); $boltforms->makeForm('testing_form'); $fields = $this->formFieldConfig(); $boltforms->addFieldArray('testing_form', $fields); $html = $boltforms->renderForm('testing_form', null, array('recaptcha' => array('enabled' => true))); $this->assertInstanceOf('\\Twig_Markup', $html); $html = (string) $html; $this->assertRegExp('#<link href="/extensions/vendor/bolt/boltforms/css/boltforms.css" rel="stylesheet" type="text/css" />#', $html); $this->assertRegExp('#<div class="boltform">#', $html); $this->assertRegExp('#var RecaptchaOptions =#', $html); $this->assertRegExp('#<form method="post" action="" name="" enctype="multipart/form-data">#', $html); $this->assertRegExp('#<ul class="boltform-error">#', $html); $this->assertRegExp('#<li class="boltform-errors"></li>#', $html); $this->assertRegExp('#<label for="form_message" class="required"></label>#', $html); $this->assertRegExp('#<script src="https://www.google.com/recaptcha/api.js\\?hl=en-GB" async defer></script>#', $html); $this->assertRegExp('#<div class="g-recaptcha" data-sitekey=""></div>#', $html); $this->assertRegExp('#<button type="submit" id="testing_form_submit" name="testing_form\\[submit\\]">Submit</button>#', $html); $this->assertRegExp('#<label for="testing_form_name" class="required">Name</label>#', $html); $this->assertRegExp('#<input type="text" id="testing_form_name" name="testing_form\\[name\\]" required="required" placeholder="Your name..." />#', $html); $this->assertRegExp('#<label for="testing_form_email" class="required">Email address</label>#', $html); $this->assertRegExp('#<input type="email" id="testing_form_email" name="testing_form\\[email\\]" required="required" placeholder="Your email..." />#', $html); $this->assertRegExp('#<label for="testing_form_message" class="required">Your message</label>#', $html); $this->assertRegExp('#<textarea id="testing_form_message" name="testing_form\\[message\\]" required="required" placeholder="Your message..." class="myclass"></textarea>#', $html); $this->assertRegExp('#<label for="testing_form_array_index">Should this test pass</label>#', $html); $this->assertRegExp('#<select id="testing_form_array_index" name="testing_form\\[array_index\\]"><option value=""></option><option value="0">Yes</option><option value="1">No</option></select>#', $html); $this->assertRegExp('#<label for="testing_form_array_assoc">What is cutest</label>#', $html); $this->assertRegExp('#<select id="testing_form_array_assoc" name="testing_form\\[array_assoc\\]"><option value=""></option><option value="kittens">Fluffy Kittens</option><option value="puppies">Cute Puppies</option></select>#', $html); $this->assertRegExp('#<label for="testing_form_lookup">Select a record</label>#', $html); $this->assertRegExp('#<select id="testing_form_lookup" name="testing_form\\[lookup\\]"><option value=""></option></select>#', $html); $this->assertRegExp('#<input type="hidden" id="testing_form__token" name="testing_form\\[_token\\]" value=#', $html); }
public function testSubmitException() { $app = $this->getApp(false); $app['dispatcher']->addListener(BoltFormsEvents::SUBMIT, array($this, 'invalidCallable')); $app->boot(); $boltforms = new BoltForms($app); $boltforms->makeForm('testing_form'); $this->formProcessRequest($app); $this->expectOutputString('Dispatch Exception'); }
protected function formProcessRequest($app) { $this->getExtension($app)->config['csrf'] = false; $app['request'] = Request::create('/'); $boltforms = new BoltForms($app); $boltforms->makeForm('testing_form'); $fields = $this->formValues(); $boltforms->addFieldArray('testing_form', $fields); $parameters = array('testing_form' => array('name' => 'Gawain Lynch', 'email' => '*****@*****.**', 'message' => 'Hello')); $app['request'] = Request::create('/', 'POST', $parameters); return $boltforms->processRequest('testing_form', array('success' => true)); }
public function testEventInvalidSetData() { $app = $this->getApp(); $app['request'] = Request::create('/'); $boltforms = new BoltForms($app); $boltforms->makeForm('testing_form'); $fields = $this->formFieldConfig(); $boltforms->addFieldArray('testing_form', $fields); $evt = new FormEvent($boltforms->getForm('testing_form'), array('koala' => 'leaves')); $event = new BoltFormsEvent($evt, FormEvents::POST_SUBMIT); $this->assertInstanceOf('Symfony\\Component\\Form\\FormEvent', $event->getEvent()); $this->assertInstanceOf('Symfony\\Component\\Form\\FormInterface', $event->getForm()); $this->assertSame(array('koala' => 'leaves'), $event->getData()); $this->setExpectedException('\\RuntimeException'); $event->getEvent()->setName(FormEvents::POST_SUBMIT); $event->setData('fail'); }
public function testSendEmailDebugFail() { $app = $this->getApp(); $this->getExtension($app)->config['csrf'] = false; $this->getExtension($app)->config['debug']['enabled'] = true; $this->getExtension($app)->config['debug']['address'] = null; $this->getExtension($app)->config['testing_form']['notification']['enabled'] = true; $app['request'] = Request::create('/'); $boltforms = new BoltForms($app); $boltforms->makeForm('testing_form'); $fields = $this->formValues(); $boltforms->addFieldArray('testing_form', $fields); $parameters = array('testing_form' => array('name' => 'Gawain Lynch', 'email' => '*****@*****.**', 'message' => 'Hello')); $app['request'] = Request::create('/', 'POST', $parameters); $this->setExpectedException('\\Bolt\\Extension\\Bolt\\BoltForms\\Exception\\EmailException', '[BoltForms] Debug email address can not be empty if debugging enabled!'); $result = $boltforms->processRequest('testing_form', array('success' => true)); $this->assertTrue($result); }
public function testWriteToContentype() { $app = $this->getApp(); $this->getExtension($app)->config['csrf'] = false; $this->getExtension($app)->config['uploads']['enabled'] = true; $this->getExtension($app)->config['uploads']['base_directory'] = __DIR__; $this->getExtension($app)->config['testing_form']['database']['contenttype'] = 'koala'; $app['request'] = Request::create('/'); $boltforms = new BoltForms($app); $boltforms->makeForm('testing_form'); $fields = $this->formValues(); $boltforms->addFieldArray('testing_form', $fields); $parameters = $this->getParameters($app); // Mock Bolt\Users $storage = $this->getMock('\\Bolt\\Storage', array('saveContent'), array($app)); $storage->expects($this->any())->method('saveContent')->willReturn(42); $app['storage'] = $storage; $app['request'] = Request::create('/', 'POST', $parameters); $result = $boltforms->processRequest('testing_form', array('success' => true)); $this->assertTrue($result); }
public function testTwigBoltFormsUploads() { $app = $this->getApp(); $this->getExtension($app)->config['uploads']['base_directory'] = sys_get_temp_dir(); $this->getExtension($app)->config['testing_form']['uploads']['subdirectory'] = 'testing_form'; $app['request'] = Request::create('/'); $boltforms = new BoltForms($app); $boltforms->makeForm('testing_form'); $fields = $this->formValues(); $boltforms->addFieldArray('testing_form', $fields); $app['boltforms'] = $boltforms; $twigExt = new BoltFormsExtension($app); // Invalid directory $html = $twigExt->twigBoltFormsUploads('koala'); $this->assertInstanceOf('\\Twig_Markup', $html); $this->assertSame('<p><strong>Invalid upload directory</strong></p>', (string) $html); // Valid $tmpDir = sys_get_temp_dir() . '/testing_form'; $srcFile = EXTENSION_TEST_ROOT . '/tests/data/bolt-logo.png'; $fs = new Filesystem(); if (!$fs->exists($tmpDir)) { $fs->mkdir($tmpDir); } $fs->copy($srcFile, "{$tmpDir}/bolt-logo.png", true); $html = $twigExt->twigBoltFormsUploads('testing_form'); $this->assertInstanceOf('\\Twig_Markup', $html); $this->assertRegExp('#<a href="/boltforms/download\\?file=bolt-logo.png">bolt-logo.png</a>#', (string) $html); }
public function testProcessRequest() { $app = $this->getApp(); $this->getExtension($app)->config['csrf'] = false; $app['request'] = Request::create('/'); $boltforms = new BoltForms($app); $boltforms->makeForm('contact'); $fields = $this->formValues(); $boltforms->addFieldArray('contact', $fields); $parameters = array('contact' => array('name' => 'Gawain Lynch', 'email' => '*****@*****.**', 'message' => 'Hello')); $app['request'] = Request::create('/', 'POST', $parameters); $result = $boltforms->processRequest('contact', array('success' => true)); $this->assertTrue($result); }
public function testRedirectRecord() { $app = $this->getApp(); $this->getExtension($app)->config['csrf'] = false; $this->getExtension($app)->config['testing_form']['feedback']['redirect']['target'] = 'page/koalas'; $this->getExtension($app)->config['testing_form']['feedback']['redirect']['query'] = 'name'; $matcher = $this->getMockBuilder('\\Bolt\\Routing\\UrlMatcher')->disableOriginalConstructor()->setMethods(array('match'))->getMock(); $matcher->expects($this->any())->method('match')->will($this->returnValue('/page/koalas')); $app['url_matcher'] = $matcher; $app['request'] = Request::create('/'); $boltforms = new BoltForms($app); $boltforms->makeForm('testing_form'); $fields = $this->formValues(); unset($fields['array_index']); unset($fields['array_assoc']); unset($fields['lookup']); $boltforms->addFieldArray('testing_form', $fields); $parameters = array('testing_form' => array('name' => 'Gawain Lynch', 'email' => '*****@*****.**', 'message' => 'Hello')); $app['request'] = Request::create('/', 'POST', $parameters); $result = $boltforms->processRequest('testing_form', array('success' => true)); $this->assertTrue($result); $this->expectOutputRegex('#<meta http-equiv="refresh" content="1;url=/page/koalas\\?name=Gawain\\+Lynch" />#'); }
public function testSessionValue() { $sessionValueField = array('type' => 'hidden', 'options' => array('label' => false), 'event' => array('name' => 'session_value', 'params' => array('key' => 'koala'))); $app = $this->getApp(false); $this->getExtension($app)->config['csrf'] = false; $this->getExtension($app)->config['testing_form']['fields']['session_value'] = $sessionValueField; $app['request'] = Request::create('/'); $boltforms = new BoltForms($app); $boltforms->makeForm('testing_form'); $fields = $this->formValues(); $fields['session_value'] = $sessionValueField; $boltforms->addFieldArray('testing_form', $fields); $parameters = array('testing_form' => array('name' => 'Gawain Lynch', 'email' => '*****@*****.**', 'message' => 'Hello')); $app['request'] = Request::create('/', 'POST', $parameters); $app->boot(); $app['session']->set('koala', 'gum-leaves'); $result = $boltforms->processRequest('testing_form', array('success' => true), true); $this->assertArrayHasKey('session_value', $result); $this->assertSame('gum-leaves', $result['session_value']); }