Allow encoding of an activity with configuration preset durations

fix #11

The activity form allow to receive pre-configured activity durations.

Those duration can be configured through the option
chill_activity.form.time_duration :

```yaml

chill_activity:
   form:
      time_duration:
         - { label: '12 minutes', seconds: 720 }
         # - ...

```

If a pre-existing activity receives a different time, the time is added
to the list of pre-configured duration time.
This commit is contained in:
2016-03-09 13:38:05 +01:00
parent f28a6e9fa0
commit 77b7333fea
9 changed files with 374 additions and 21 deletions

View File

@@ -114,10 +114,7 @@ class ActivityControllerTest extends WebTestCase
$form = $crawler->selectButton('Ajouter une nouvelle activité')->form(array(
'chill_activitybundle_activity'=> array(
'date' => '15-01-2015',
'durationTime' => array(
'hour' => '1',
'minute' => '30'
),
'durationTime' => 600,
'remark' => 'blabla',
'scope' => $this->getRandomScope('center a_social', 'Center A')->getId(),
'type' => $this->getRandomActivityType()->getId()

View File

@@ -0,0 +1,205 @@
<?php
/*
* Copyright (C) 2016 Champs-Libres <info@champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Chill\ActivityBundle\Tests\Form;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Chill\ActivityBundle\Form\ActivityType;
use Chill\ActivityBundle\Entity\Activity;
use Symfony\Component\Security\Core\Authentication\Token\AbstractToken;
use Symfony\Component\Security\Core\Role\Role;
use Symfony\Component\Form\Extension\Core\Type\FormType;
/**
*
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class ActivityTypeTest extends KernelTestCase
{
/**
*
* @var \Symfony\Component\Form\FormBuilderInterface
*/
protected $formBuilder;
/**
*
* @var \Symfony\Component\DependencyInjection\ContainerInterface
*/
protected $container;
/**
*
* @var \Symfony\Component\Security\Core\User\UserInterface
*/
protected $user;
/**
*
* @var \Chill\MainBundle\Entity\Center
*/
protected $center;
public function setUp()
{
self::bootKernel();
$this->container = self::$kernel->getContainer();
$prophet = new \Prophecy\Prophet;
$this->formBuilder = $this->container
->get('form.factory')
->createBuilder(FormType::class, null, array(
'csrf_protection' => false,
'csrf_field_name' => '_token'
));
$request = new \Symfony\Component\HttpFoundation\Request();
$request->setLocale('fr');
self::$kernel->getContainer()
->get('request_stack')
->push($request);
$this->user = $this->container->get('doctrine.orm.entity_manager')
->getRepository('ChillMainBundle:User')
->findOneBy(array('username' => 'center a_social'));
$this->center = $this->container->get('doctrine.orm.entity_manager')
->getRepository('ChillMainBundle:Center')
->findOneBy(array('name' => 'Center A'));
$token = $prophet->prophesize();
$token->willExtend(AbstractToken::class);
$token->getUser()->willReturn($this->user);
$this->container->get('security.token_storage')
->setToken($token->reveal());
}
public function testForm()
{
$form = $this->formBuilder
->add('activity', ActivityType::class, array(
'center' => $this->center,
'role' => new Role('CHILL_ACTIVITY_CREATE')
))
->getForm();
$form->submit(array());
$this->assertTrue($form->isSynchronized());
$this->assertTrue($form->isValid());
$this->assertInstanceOf(Activity::class, $form->getData()['activity']);
}
public function testFormSubmitting()
{
$form = $this->formBuilder
->add('activity', ActivityType::class, array(
'center' => $this->center,
'role' => new Role('CHILL_ACTIVITY_CREATE')
))
->getForm();
$form->submit(array( 'activity' => array(
'date' => '9-3-2015',
'durationTime' => 300,
'remark' => 'blabla',
'attendee' => true
)));
// var_dump($form->getErrors()->count()); var_dump($form->isValid());
// foreach($form->getErrors() as $e) { fwrite(STDOUT, var_dump($e->getMessage())); }
// var_dump($form->getErrors());
$this->assertTrue($form->isSynchronized(), "Test the form is synchronized");
$this->assertTrue($form->isValid(), "test the form is valid");
$this->assertInstanceOf(Activity::class, $form->getData()['activity']);
// test the activity
/* @var $activity Activity */
$activity = $form->getData()['activity'];
$this->assertEquals('09-03-2015', $activity->getDate()->format('d-m-Y'),
"Test the date is correct");
$this->assertEquals('00:05', $activity->getDurationTime()->format('H:i'),
"Test the formatted hour is correct");
$this->assertEquals(true, $activity->getAttendee());
$this->assertEquals('blabla', $activity->getRemark());
}
/**
* Test that the form correctly build even with a durationTime which is not in
* the listed in the possible durationTime
*/
public function testFormWithActivityHavingDifferentTime()
{
$activity = new Activity();
$activity->setDurationTime(\DateTime::createFromFormat('U', 60));
$builder = $this->container
->get('form.factory')
->createBuilder(FormType::class, array('activity' => $activity), array(
'csrf_protection' => false,
'csrf_field_name' => '_token'
));
$form = $builder
->add('activity', ActivityType::class, array(
'center' => $this->center,
'role' => new Role('CHILL_ACTIVITY_CREATE')
))
->getForm();
$form->submit(array( 'activity' => array(
'date' => '9-3-2015',
'durationTime' => 60,
'remark' => 'blabla',
'attendee' => true
)));
$this->assertTrue($form->isSynchronized());
$this->assertTrue($form->isValid());
// test the activity
/* @var $activity Activity */
$activity = $form->getData()['activity'];
$this->assertEquals('00:01', $activity->getDurationTime()->format('H:i'),
"Test the formatted hour is correct");
// test the view : we want to be sure that the entry with 60 seconds exists
$view = $form->createView();
$this->assertTrue(isset($view['activity']['durationTime']));
// map all the values in an array
$values = array_map(function($choice) { return $choice->value; },
$view['activity']['durationTime']->vars['choices']);
$this->assertContains(60, $values);
}
}