Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Behat tests #25

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions Resolver/WidgetFormContentResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Victoire\Widget\FormBundle\Resolver;

use Symfony\Component\Intl\DateFormatter\IntlDateFormatter;
use Victoire\Bundle\CoreBundle\Helper\CurrentViewHelper;
use Victoire\Bundle\WidgetBundle\Model\Widget;
use Victoire\Bundle\WidgetBundle\Resolver\BaseWidgetContentResolver;
use Victoire\Widget\FormBundle\Entity\WidgetForm;

class WidgetFormContentResolver extends BaseWidgetContentResolver
{
protected $currentViewHelper;

/**
* WidgetFormContentResolver constructor.
*
* @param CurrentViewHelper $currentViewHelper
*/
public function __construct(CurrentViewHelper $currentViewHelper) {
$this->currentViewHelper = $currentViewHelper;
}

/**
* Get the static content of the widget.
*
* @param Widget $widget
*
* @return string
*/
public function getWidgetStaticContent(Widget $widget)
{
$parameters = parent::getWidgetStaticContent($widget);
$parameters = self::addMonthParameters($widget, $parameters);

return $parameters;
}

protected function addMonthParameters(Widget $widget, $parameters)
{
$currentView = $this->currentViewHelper->getCurrentView();
$locale = $currentView->getCurrentLocale();
$formatter = new IntlDateFormatter($locale, 0, 0);

$months = [];
$date = new \DateTime();
foreach (range(1, 12) as $monthNumber) {
$date->setDate(2012, $monthNumber, 1);
$formatter->setPattern('MMMM');
$months[$monthNumber] = $formatter->format($date);
}

$parameters = array_merge($parameters, [
'months' => $months
]);

return $parameters;
}
}
5 changes: 4 additions & 1 deletion Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
services:
victoire_core.widget_form_content_resolver:
class: Victoire\Bundle\WidgetBundle\Resolver\BaseWidgetContentResolver
parent: victoire_widget.base_widget_content_resolver
class: Victoire\Widget\FormBundle\Resolver\WidgetFormContentResolver
arguments:
- '@victoire_core.current_view'
tags:
- { name: victoire_widget.widget_content_resolver, alias: Form }

Expand Down
2 changes: 1 addition & 1 deletion Resources/public/js/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ $(document).ready(function () {
var form_group = $(this).parents('.form-group');
if(re.test($(this).val()) !== true) {
form_group.addClass('has-error');
form_group.find('.help-block').text($(this).val() + ' n\'est pas un email valide').show();
form_group.find('.help-block').text(Translator.trans('victoire_widget_form.email.not.valid', {'email' : $(this).val()})).show();
event.preventDefault();
} else {
form_group.removeClass('has-error');
Expand Down
4 changes: 4 additions & 0 deletions Resources/translations/messages.en.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@
<source>victoire_widget_form.message.admin.newMessage</source>
<target>New message:</target>
</trans-unit>
<trans-unit id="58e25b0a67145" resname="victoire_widget_form.email.not.valid">
<source>victoire_widget_form.email.not.valid</source>
<target>%email% is not a valid email</target>
</trans-unit>
</body>
</file>
</xliff>
4 changes: 4 additions & 0 deletions Resources/translations/messages.es.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@
<source>victoire_widget_form.message.admin.newMessage</source>
<target>Nuevo puesto:</target>
</trans-unit>
<trans-unit id="58e25b0a67145" resname="victoire_widget_form.email.not.valid">
<source>victoire_widget_form.email.not.valid</source>
<target>%email% no es una dirección de e-mail valida</target>
</trans-unit>
</body>
</file>
</xliff>
4 changes: 4 additions & 0 deletions Resources/translations/messages.fr.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@
<source>victoire_widget_form.message.admin.newMessage</source>
<target>Nouveau message:</target>
</trans-unit>
<trans-unit id="58e25b0a67145" resname="victoire_widget_form.email.not.valid">
<source>victoire_widget_form.email.not.valid</source>
<target>%email% n'est pas un email valide</target>
</trans-unit>
</body>
</file>
</xliff>
18 changes: 9 additions & 9 deletions Resources/translations/victoire.en.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -214,23 +214,23 @@
<source>widget_form.form.choice.style.label.default</source>
<target>Default</target>
</trans-unit>
<trans-unit resname="widget_form.form.choice.style.label.primary" id="58e3ad09e9d32">
<trans-unit resname="widget_form.form.choice.style.label.primary" id="58e3ad0a06334">
<source>widget_form.form.choice.style.label.primary</source>
<target>Principal</target>
<target>Primary</target>
</trans-unit>
<trans-unit resname="widget_form.form.choice.style.label.success" id="58e3ad09e9e8a">
<trans-unit resname="widget_form.form.choice.style.label.success" id="58e3ad0a063b9">
<source>widget_form.form.choice.style.label.success</source>
<target>Succès</target>
<target>Succes</target>
</trans-unit>
<trans-unit resname="widget_form.form.choice.style.label.info" id="58e3ad09e9f29">
<trans-unit resname="widget_form.form.choice.style.label.info" id="58e3ad0a06528">
<source>widget_form.form.choice.style.label.info</source>
<target>Informatif</target>
<target>Informative</target>
</trans-unit>
<trans-unit resname="widget_form.form.choice.style.label.warning" id="58e3ad09e9fb2">
<trans-unit resname="widget_form.form.choice.style.label.warning" id="58e3ad0a06591">
<source>widget_form.form.choice.style.label.warning</source>
<target>Avertissement</target>
<target>Warning</target>
</trans-unit>
<trans-unit resname="widget_form.form.choice.style.label.danger" id="58e3ad09ea039">
<trans-unit resname="widget_form.form.choice.style.label.danger" id="58e3ad0a065fc">
<source>widget_form.form.choice.style.label.danger</source>
<target>Danger</target>
</trans-unit>
Expand Down
18 changes: 9 additions & 9 deletions Resources/translations/victoire.fr.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -242,23 +242,23 @@
<source>widget_form.form.choice.style.label.default</source>
<target>Par défaut</target>
</trans-unit>
<trans-unit resname="widget_form.form.choice.style.label.primary" id="58e3ad0a06334">
<trans-unit resname="widget_form.form.choice.style.label.primary" id="58e3ad09e9d32">
<source>widget_form.form.choice.style.label.primary</source>
<target>Primary</target>
<target>Principal</target>
</trans-unit>
<trans-unit resname="widget_form.form.choice.style.label.success" id="58e3ad0a063b9">
<trans-unit resname="widget_form.form.choice.style.label.success" id="58e3ad09e9e8a">
<source>widget_form.form.choice.style.label.success</source>
<target>Succes</target>
<target>Succès</target>
</trans-unit>
<trans-unit resname="widget_form.form.choice.style.label.info" id="58e3ad0a06528">
<trans-unit resname="widget_form.form.choice.style.label.info" id="58e3ad09e9f29">
<source>widget_form.form.choice.style.label.info</source>
<target>Informative</target>
<target>Informatif</target>
</trans-unit>
<trans-unit resname="widget_form.form.choice.style.label.warning" id="58e3ad0a06591">
<trans-unit resname="widget_form.form.choice.style.label.warning" id="58e3ad09e9fb2">
<source>widget_form.form.choice.style.label.warning</source>
<target>Warning</target>
<target>Avertissement</target>
</trans-unit>
<trans-unit resname="widget_form.form.choice.style.label.danger" id="58e3ad0a065fc">
<trans-unit resname="widget_form.form.choice.style.label.danger" id="58e3ad09ea039">
<source>widget_form.form.choice.style.label.danger</source>
<target>Danger</target>
</trans-unit>
Expand Down
4 changes: 2 additions & 2 deletions Resources/views/formCreatorQuestion.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@
</div>
</div>
<div class="question_{{ name }}_proposal question_{{ name }}_" style="display: none">
<ul id="question_{{ name }}_proposals-form-list" data-prototype="<div class='vic-input-group'><input type='text' class='vic-form-control' id ='static_widget_form_questions_{{ name }}_proposal__nameProposal__' name='_static_widget_form[questions][{{ name }}][proposal][__nameProposal__]' value=''></div>">
<ul id="question_{{ name }}_proposals-form-list" data-prototype="<div class='vic-input-group'><input type='text' class='vic-form-control' id ='{{ quantum }}_static_widget_form_questions_{{ name }}_proposal__nameProposal__' name='_{{ quantum }}_static_widget_form[questions][{{ name }}][proposal][__nameProposal__]' value=''></div>">

{% for proposal in proposals %}
<li class="proposals{{ name }}">
<div class='vic-input-group'>
<input type="text" id ="static_widget_form_questions_{{ name }}_proposal_{{ loop.index0 }}" class="vic-form-control" value="{{ proposal }}" name="_static_widget_form[questions][{{ name }}][proposal][{{ loop.index0 }}]">
<input type="text" id ="{{ quantum }}_static_widget_form_questions_{{ name }}_proposal_{{ loop.index0 }}" class="vic-form-control" value="{{ proposal }}" name="_{{ quantum }}_static_widget_form[questions][{{ name }}][proposal][{{ loop.index0 }}]">
</div>
</li>
{% endfor %}
Expand Down
10 changes: 5 additions & 5 deletions Resources/views/new.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
</div>
<div class="vic-form-group vic-well">
{{ form_row(form.successCallback) }}
<div id="widget-form-success-notification" {{ (form.successCallback.vars.value == 'notification' or form.successCallback.vars.value == '') ? '' : 'class="hidden"' }}>
{{ form_row(form.successMessage) }}
</div>
<div id="widget-form-success-redirect" {{ form.successCallback.vars.value == 'redirect' ? '' : 'class="hidden"' }}>
{{ form_widget(form.link) }}
</div>
<div id="widget-form-success-notification" {{ form.successCallback.vars.value == 'notification' ? '' : 'class="hidden"' }}>
{{ form_row(form.successMessage) }}
</div>
{{ form_row(form.targetEmail) }}
{{ form_row(form.adminSubject) }}
</div>
Expand Down Expand Up @@ -71,13 +71,13 @@
CKEDITOR.instances[instance].updateElement();
}
});
$vic('#static_widget_form_successCallback').on('change', function() {
$vic('#{{ quantum }}_static_widget_form_successCallback').on('change', function() {
$vic('#widget-form-success-notification, #widget-form-success-redirect').addClass('hidden');
$vic('#widget-form-success-' + $vic(this).val()).removeClass('hidden');
});
$vic(document).ready(function() {

$vic('#static_widget_form_errorNotification').on('click', function() {
$vic('#{{ quantum }}_static_widget_form_errorNotification').on('click', function() {
$vic('#widget-form-error-messsage').toggleClass('hidden');
});
});
Expand Down
2 changes: 0 additions & 2 deletions Resources/views/show.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@
{% if question.prefix %}
</div>
{% endif %}
{{ question.type }}
</label>
</div>
{% elseif question.type == "date" %}
Expand All @@ -102,7 +101,6 @@
{% endfor %}
</select>
<select class="form-control" style="width: auto;float:left;margin:0 5px;" name="{{ formName }}[questions][{{ questionId }}][Month]"{% if question.required %}required="required"{% endif %}>
{% set months = {'1' : 'Janvier','2' : 'Février','3' : 'Mars','4' : 'Avril','5' : "Mai", '6' : "Juin", '7' : "Juillet", '8' : "Aout", '9' : "Septembre", '10' : "Octobre", '11' : "Novembre", '12' : "Décembre"} %}
{% for key, month in months %}
<option value="{{ month }}"{% if key == "now"|date('n') %} selected="selected"{% endif %}>{{ month }}</option>
{% endfor %}
Expand Down
153 changes: 153 additions & 0 deletions Tests/Context/WidgetContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php

namespace Victoire\Widget\FormBundle\Tests\Context;

use Knp\FriendlyContexts\Context\RawMinkContext;

class WidgetContext extends RawMinkContext
{
/**
* @When /^I add a question to the form$/
*/
public function iAddAQuestionToTheForm()
{
$page = $this->getSession()->getPage();

$link = $page->find('xpath', 'descendant-or-self::a[contains(@class, "add_question_link")]');

if (null === $link) {
$message = 'The link to add a question could not be found.';
throw new \Behat\Mink\Exception\ResponseTextException($message, $this->getSession());
}

$link->click();
}

/**
* @Then /^I should see submit button "(.+)" with icon "(.+)" and style "(.+)"$/
*/
public function iShouldSeeSubmitButtonWithIconAndStyle($text, $icon, $style)
{
$page = $this->getSession()->getPage();

$button = $page->find('xpath', sprintf(
'descendant-or-self::button[contains(@class, "btn-%s") and @type = "submit" and contains(normalize-space(.), "%s")]/span[contains(@class, "%s")]',
strtolower($style),
$text,
$icon
));

if (null === $button) {
$message = sprintf(
'Submit button with text "%s", icon "%s" and style "%s" could not be found.',
$text,
$icon,
$style
);
throw new \Behat\Mink\Exception\ResponseTextException($message, $this->getSession());
}
}

/**
* @When /^I add a choice for question "(.+)"$/
*/
public function iAddAChoiceForQuestion($questionNb)
{
$page = $this->getSession()->getPage();

$link = $page->find('xpath', sprintf(
'descendant-or-self::a[contains(@class, "add_proposal%s_link")]',
$questionNb
));

if (null === $link) {
$message = 'The link to add a question could not be found.';
throw new \Behat\Mink\Exception\ResponseTextException($message, $this->getSession());
}

$link->click();
}

/**
* @When /^I should see (a simple input|a textarea|a date input|an email input|a checkbox|a multiple choice|a single choice) for question "(.+)"$/
*/
public function iShouldSeeAFormElementForQuestion($formElement, $questionNb)
{
$questionNb--;

$page = $this->getSession()->getPage();

switch ($formElement) {
case 'a simple input':
$xpaths = ['descendant-or-self::input[@type = "text" and @name = "cms_form_content[questions][%s][]"]'];
break;
case 'a textarea':
$xpaths = ['descendant-or-self::textarea[@type = "textarea" and @name = "cms_form_content[questions][%s][]"]'];
break;
case 'a date input':
$xpaths = [
'descendant-or-self::select[@name = "cms_form_content[questions][%s][Day]"]',
'descendant-or-self::select[@name = "cms_form_content[questions][%s][Month]"]',
'descendant-or-self::select[@name = "cms_form_content[questions][%s][Year]"]',
];
break;
case 'an email input':
$xpaths = ['descendant-or-self::input[@type = "email" and @name = "cms_form_content[questions][%s][]"]'];
break;
case 'a checkbox':
$xpaths = ['descendant-or-self::input[@type = "checkbox" and @name = "cms_form_content[questions][%s][]"]'];
break;
case 'a multiple choice':
$xpaths = ['descendant-or-self::select[@multiple and @name = "cms_form_content[questions][%s][proposal][]"]'];
break;
case 'a single choice':
$xpaths = ['descendant-or-self::select[not(@multiple) and @name = "cms_form_content[questions][%s][proposal][]"]'];
break;
}

foreach ($xpaths as $xpath) {
$formElement = $page->find('xpath', sprintf($xpath, $questionNb));

if (null === $formElement) {
$message = sprintf(
'The form element for question %s could not be found.',
$questionNb + 1
);
throw new \Behat\Mink\Exception\ResponseTextException($message, $this->getSession());
}
}
}

/**
* @When /^I should see choice "(.+)" for (multiple|single) choice "(.+)"$/
*/
public function iShouldSeeMultipleChoiceForQuestion($text, $formElement, $questionNb)
{
$questionNb--;

$page = $this->getSession()->getPage();

switch ($formElement) {
case 'multiple':
$xpath = 'descendant-or-self::select[@multiple and @name = "cms_form_content[questions][%s][proposal][]"]//option[contains(normalize-space(.), "%s")]';
break;
case 'single':
$xpath = 'descendant-or-self::select[not(@multiple) and @name = "cms_form_content[questions][%s][proposal][]"]//option[contains(normalize-space(.), "%s")]';
break;
}

$formElement = $page->find('xpath', sprintf(
$xpath,
$questionNb,
$text
));

if (null === $formElement) {
$message = sprintf(
'The choice element for question %s could not be found.',
$questionNb + 1
);
throw new \Behat\Mink\Exception\ResponseTextException($message, $this->getSession());
}
}
}
Loading