Functional test for timetable schemas
=====================================


Overview
--------

1. Add a timetable schema named 'weekly' with the advanced add view.
2. Add another timetable schema -- one called 'Schema 2'.
3. Choose the second schema as the default one.
4. Add a third timetable schema ('other') -- but this time use the simple add
   view.
5. Create a term '2005-summer' from 2005-06-01 to 2005-08-31.
6. Mark 2005-07-07 as a special day -- change class start and end times.
7. Switch the default schema to 'other'.
8. Mark 2005-07-07 as an emergency day -- move it to 2005-09-01.
9. Verify that 2005-09-01 remembers class times specified in step 6.
10. Delete a school timetable
11. Delete a term


Setup
-----

    >>> from schooltool.app.browser.ftests.setup import setUpBasicSchool
    >>> manager = Browser('manager', 'schooltool')

    >>> manager.getLink('Manage').click()
    >>> manager.getLink('School Years').click()
    >>> manager.getLink('New School Year').click()
    >>> manager.getControl('Title').value = '2005'
    >>> manager.getControl('First day').value = '2005-05-01'
    >>> manager.getControl('Last day').value = '2005-10-15'
    >>> manager.getControl('Add').click()

The schema container is empty initially:

    >>> manager.getLink('2005').click()
    >>> manager.getLink('School Timetables').click()
    >>> '<p>There are none.</p>' in manager.contents
    True


Advanced timetable schema add view
----------------------------------

Let's go to the advanced 'New Timetable Schema' view:

    >>> manager.getLink('2005').click()
    >>> manager.getLink('School Timetables').click()
    >>> manager.getLink('New Timetable').click()
    >>> manager.getLink('advanced adding form').click()

When we enter a schema name and press the 'Add day' button, we get an
extra day:

    >>> manager.getControl('Title').value = 'weekly'
    >>> manager.getControl('Add day').click()
    >>> 'Day 2' in manager.contents
    True

When we click 'Add Period', we get an extra period!

    >>> manager.getControl('Add period').click()
    >>> 'Period 2' in manager.contents
    True

Finally we post the completed form:

Make sure we have five days

    >>> manager.getControl('Add day').click()
    >>> manager.getControl('Add day').click()
    >>> manager.getControl('Add day').click()

Make sure we have 4 periods

    >>> manager.getControl('Add period').click()
    >>> manager.getControl('Add period').click()

    >>> manager.getControl(name='day1').value = 'Monday'
    >>> manager.getControl(name='day1.period1').value = 'Period 1'
    >>> manager.getControl(name='day1.period2').value = 'Period 2'
    >>> manager.getControl(name='day1.period3').value = 'Period 3'
    >>> manager.getControl(name='day1.period4').value = 'Period 4'

    >>> manager.getControl(name='day2').value = 'Tuesday'
    >>> manager.getControl(name='day2.period1').value = 'Period 1'
    >>> manager.getControl(name='day2.period2').value = 'Period 2'
    >>> manager.getControl(name='day2.period3').value = 'Period 3'
    >>> manager.getControl(name='day2.period4').value = 'Period 4'

    >>> manager.getControl(name='day3').value = 'Wednesday'
    >>> manager.getControl(name='day3.period1').value = 'Period 1'
    >>> manager.getControl(name='day3.period2').value = 'Period 2'
    >>> manager.getControl(name='day3.period3').value = 'Period 3'
    >>> manager.getControl(name='day3.period4').value = 'Period 4'

    >>> manager.getControl(name='day4').value = 'Thursday'
    >>> manager.getControl(name='day4.period1').value = 'Period 1'
    >>> manager.getControl(name='day4.period2').value = 'Period 2'
    >>> manager.getControl(name='day4.period3').value = 'Period 3'
    >>> manager.getControl(name='day4.period4').value = 'Period 4'

    >>> manager.getControl(name='day5').value = 'Friday'
    >>> manager.getControl(name='day5.period1').value = 'Period 1'
    >>> manager.getControl(name='day5.period2').value = 'Period 2'
    >>> manager.getControl(name='day5.period3').value = 'Period 3'
    >>> manager.getControl(name='day5.period4').value = 'Period 4'

    >>> manager.getControl('Update', index=1).click()
    >>> manager.getControl('Timetable day always coincides with the').click()

    >>> manager.getControl(name='time1.day0').value = '9:00-9:45'
    >>> manager.getControl(name='time1.day1').value = '9:00-9:45'
    >>> manager.getControl(name='time1.day2').value = '9:00-9:45'
    >>> manager.getControl(name='time1.day3').value = '9:00-9:45'
    >>> manager.getControl(name='time1.day5').value = '9:00-9:45'
    >>> manager.getControl(name='time1.day6').value = '9:00-9:45'

    >>> manager.getControl(name='time2.day0').value = '10:00-10:45'
    >>> manager.getControl(name='time2.day1').value = '10:00-10:45'
    >>> manager.getControl(name='time2.day2').value = '10:00-10:45'
    >>> manager.getControl(name='time2.day3').value = '10:00-10:45'
    >>> manager.getControl(name='time2.day5').value = '10:00-10:45'
    >>> manager.getControl(name='time2.day6').value = '10:00-10:45'

    >>> manager.getControl(name='time3.day0').value = '11:00-11:45'
    >>> manager.getControl(name='time3.day1').value = '11:00-11:45'
    >>> manager.getControl(name='time3.day2').value = '11:00-11:45'
    >>> manager.getControl(name='time3.day3').value = '11:00-11:45'
    >>> manager.getControl(name='time3.day5').value = '11:00-11:45'
    >>> manager.getControl(name='time3.day6').value = '11:00-11:45'

    >>> manager.getControl(name='time4.day0').value = '12:00-12:45'
    >>> manager.getControl(name='time4.day1').value = '12:00-12:45'
    >>> manager.getControl(name='time4.day2').value = '12:00-12:45'
    >>> manager.getControl(name='time4.day3').value = '12:00-12:45'
    >>> manager.getControl(name='time4.day5').value = '12:00-12:45'
    >>> manager.getControl(name='time4.day6').value = '12:00-12:45'

    >>> manager.getControl('Duration').value = '45'
    >>> manager.getControl('Create timetable schema').click()

Now we see the newly created timetable schema in the list, it should
be the default one:

    >>> '<option selected="selected" value="weekly">' in manager.contents
    True

Lets add one more schema:

    >>> manager.getLink('New Timetable').click()
    >>> manager.getLink('advanced adding form').click()
    >>> manager.getControl('Title').value = 'Schema 2'

Add enough days

    >>> manager.getControl('Add day').click()
    >>> manager.getControl('Add day').click()
    >>> manager.getControl('Add day').click()
    >>> manager.getControl('Add day').click()

Have enough periods

    >>> manager.getControl('Add period').click()
    >>> manager.getControl('Add period').click()
    >>> manager.getControl('Add period').click()

    >>> manager.getControl(name='day1').value = 'Monday'
    >>> manager.getControl(name='day1.period1').value = 'Period 1'
    >>> manager.getControl(name='day1.period2').value = 'Period 2'
    >>> manager.getControl(name='day1.period3').value = 'Period 3'
    >>> manager.getControl(name='day1.period4').value = 'Period 4'

    >>> manager.getControl(name='day2').value = 'Tuesday'
    >>> manager.getControl(name='day2.period1').value = 'Period 1'
    >>> manager.getControl(name='day2.period2').value = 'Period 2'
    >>> manager.getControl(name='day2.period3').value = 'Period 3'
    >>> manager.getControl(name='day2.period4').value = 'Period 4'

    >>> manager.getControl(name='day3').value = 'Wednesday'
    >>> manager.getControl(name='day3.period1').value = 'Period 1'
    >>> manager.getControl(name='day3.period2').value = 'Period 2'
    >>> manager.getControl(name='day3.period3').value = 'Period 3'
    >>> manager.getControl(name='day3.period4').value = 'Period 4'

    >>> manager.getControl(name='day4').value = 'Thursday'
    >>> manager.getControl(name='day4.period1').value = 'Period 1'
    >>> manager.getControl(name='day4.period2').value = 'Period 2'
    >>> manager.getControl(name='day4.period3').value = 'Period 3'
    >>> manager.getControl(name='day4.period4').value = 'Period 4'

    >>> manager.getControl(name='day5').value = 'Friday'
    >>> manager.getControl(name='day5.period1').value = 'Period 1'
    >>> manager.getControl(name='day5.period2').value = 'Period 2'
    >>> manager.getControl(name='day5.period3').value = 'Period 3'
    >>> manager.getControl(name='day5.period4').value = 'Period 4'

    >>> manager.getControl('Update', index=1).click()
    >>> manager.getControl('Timetable day always coincides with the').click()

    >>> manager.getControl(name='time1.day0').value = '9:00-9:45'
    >>> manager.getControl(name='time1.day1').value = '9:00-9:45'
    >>> manager.getControl(name='time1.day2').value = '9:00-9:45'
    >>> manager.getControl(name='time1.day3').value = '9:00-9:45'
    >>> manager.getControl(name='time1.day5').value = '9:00-9:45'
    >>> manager.getControl(name='time1.day6').value = '9:00-9:45'

    >>> manager.getControl(name='time2.day0').value = '10:00-10:45'
    >>> manager.getControl(name='time2.day1').value = '10:00-10:45'
    >>> manager.getControl(name='time2.day2').value = '10:00-10:45'
    >>> manager.getControl(name='time2.day3').value = '10:00-10:45'
    >>> manager.getControl(name='time2.day5').value = '10:00-10:45'
    >>> manager.getControl(name='time2.day6').value = '10:00-10:45'

    >>> manager.getControl(name='time3.day0').value = '11:00-11:45'
    >>> manager.getControl(name='time3.day1').value = '11:00-11:45'
    >>> manager.getControl(name='time3.day2').value = '11:00-11:45'
    >>> manager.getControl(name='time3.day3').value = '11:00-11:45'
    >>> manager.getControl(name='time3.day5').value = '11:00-11:45'
    >>> manager.getControl(name='time3.day6').value = '11:00-11:45'

    >>> manager.getControl(name='time4.day0').value = '12:00-12:45'
    >>> manager.getControl(name='time4.day1').value = '12:00-12:45'
    >>> manager.getControl(name='time4.day2').value = '12:00-12:45'
    >>> manager.getControl(name='time4.day3').value = '12:00-12:45'
    >>> manager.getControl(name='time4.day5').value = '12:00-12:45'
    >>> manager.getControl(name='time4.day6').value = '12:00-12:45'

    >>> manager.getControl('Duration').value = '45'
    >>> manager.getControl('Create timetable schema').click()

It should appear on the list:

    >>> '<option value="schema-2">' in manager.contents
    True

We can change the default ttschema if we want to:

    >>> manager.getControl('Schema 2').click()
    >>> manager.getControl('Change').click()

    >>> '<option selected="selected" value="schema-2">' in manager.contents
    True


Simple timetable add view
-------------------------

Now let's try the simple timetable schema setup view:

    >>> manager.getLink('New Timetable').click()
    >>> manager.getLink('weekly timetable schema').click()
    >>> manager.getControl('Title').value = 'other'

    >>> manager.getControl(name='field.period_name_1').value = 'P1'
    >>> manager.getControl(name='field.period_start_1').value = '9:00'
    >>> manager.getControl(name='field.period_finish_1').value = '9:45'

    >>> manager.getControl(name='field.period_name_2').value = 'P2'
    >>> manager.getControl(name='field.period_start_2').value = '10:00'
    >>> manager.getControl(name='field.period_finish_2').value = '10:45'

    >>> manager.getControl(name='field.period_name_3').value = 'P3'
    >>> manager.getControl(name='field.period_start_3').value = '11:00'
    >>> manager.getControl(name='field.period_finish_3').value = '11:45'

    >>> manager.getControl('Create').click()

A new timetable schema appeared:

    >>> '<option value="other">' in manager.contents
    True


Special days
------------

We need a term:

    >>> manager.getLink('2005').click()
    >>> manager.getLink('Add a new term').click()
    >>> manager.getControl('Title').value = '2005 Summer'
    >>> manager.getControl('Start date').value = '2005-06-01'
    >>> manager.getControl('End date').value = '2005-08-31'
    >>> manager.getControl('Next').click()

    >>> manager.getControl('Saturday').click()
    >>> manager.getControl('Sunday').click()

    >>> manager.getControl('Add term').click()

We can shift the start and end times for periods for a certain day:

    >>> manager.getLink('2005').click()
    >>> manager.getLink('School Timetables').click()
    >>> manager.getLink('other').click()
    >>> manager.getLink('Change Timetable for One Day').click()

    >>> manager.getControl('Date').value = '2005-07-07'
    >>> manager.getControl('Proceed').click()

If we fill in this form, an exception day is posted:

    >>> manager.getControl(name='P1_start').value = '8:00'
    >>> manager.getControl(name='P1_end').value = '8:30'
    >>> manager.getControl(name='P2_start').value = '8:45'
    >>> manager.getControl(name='P2_end').value = '9:15'
    >>> manager.getControl(name='P3_start').value = '9:30'
    >>> manager.getControl(name='P3_end').value = '10:15'

    >>> manager.getControl('Modify').click()

If we look at the form again, we see the times have changed:

    >>> manager.getLink('Change Timetable for One Day').click()
    >>> manager.getControl('Date').value = '2005-07-07'
    >>> manager.getControl('Proceed').click()

    >>> manager.getControl(name='P1_start').value
    '08:00'
    >>> manager.getControl(name='P1_end').value
    '08:30'
    >>> manager.getControl(name='P2_start').value
    '08:45'
    >>> manager.getControl(name='P2_end').value
    '09:15'
    >>> manager.getControl(name='P3_start').value
    '09:30'
    >>> manager.getControl(name='P3_end').value
    '10:15'


Emergency Days
--------------

Now let's make 2005-07-07 an emergency day:

    >>> manager.getLink('2005').click()
    >>> manager.getLink('2005 Summer').click()
    >>> manager.getLink('Cancel School in Emergency').click()

    >>> manager.getControl('Date').value = '2005-07-07'
    >>> manager.getControl('Proceed').click()

When we select the second date, the replacement day is registered:

    >>> manager.getControl('Replacement').getControl('2005-09-01').click()
    >>> manager.getControl('Proceed').click()

Now all periods for this day are cancelled:

    >>> manager.getLink('2005').click()
    >>> manager.getLink('School Timetables').click()
    >>> manager.getLink('other').click()
    >>> manager.getLink('Change Timetable for One Day').click()

    >>> manager.getControl('Date').value = '2005-07-07'
    >>> manager.getControl('Proceed').click()

    >>> manager.getControl(name='P1_start').value
    ''
    >>> manager.getControl(name='P1_end').value
    ''
    >>> manager.getControl(name='P2_start').value
    ''
    >>> manager.getControl(name='P2_end').value
    ''
    >>> manager.getControl(name='P3_start').value
    ''
    >>> manager.getControl(name='P3_end').value
    ''

But the replacement day now contains periods all-right:

    >>> manager.getLink('Change Timetable for One Day').click()

    >>> manager.getControl('Date').value = '2005-09-01'
    >>> manager.getControl('Proceed').click()

    >>> manager.getControl(name='P1_start').value
    '09:00'
    >>> manager.getControl(name='P1_end').value
    '09:45'
    >>> manager.getControl(name='P2_start').value
    '10:00'
    >>> manager.getControl(name='P2_end').value
    '10:45'
    >>> manager.getControl(name='P3_start').value
    '11:00'
    >>> manager.getControl(name='P3_end').value
    '11:45'

Let's go back to the index of the school timetables:

    >>> manager.getLink('2005').click()
    >>> manager.getLink('School Timetables').click()

Let's delete the new schema:

    >>> manager.getControl(name='delete.schema-2').value = True
    >>> manager.getControl('Delete').click()

We get to the confirmation page:

    >>> print manager.contents
    <BLANKLINE>
    ...
    Are you sure you want to delete the selected school timetables?
    ...
            <input type="hidden" name="delete.schema-2" />
            <a href="http://localhost/schoolyears/2005/school_timetables/schema-2">Schema 2</a>.
            This school timetable is used by 0 individual timetables.
    ...

Let's proceed:

    >>> manager.getControl('Confirm').click()

We get back to the school timetables index page:

    >>> '<h1>School Timetables</h1>' in manager.contents
    True

But the deleted schema is not there:

    >>> 'schema-2' not in manager.contents
    True
    >>> 'Schema 2' not in manager.contents
    True

The default schema selection is unset:

    >>> 'selected' not in manager.contents
    True


Let's go back to the index of terms:

    >>> manager.getLink('2005').click()

Let's delete the term:

    >>> manager.getControl(name='delete.2005-summer').value = True
    >>> manager.getControl('Delete').click()

We get to the confirmation page:

    >>> print manager.contents
    <BLANKLINE>
    ...
    Are you sure you want to delete the selected terms?
    ...
            <input type="hidden" name="delete.2005-summer" />
            <a href="http://localhost/schoolyears/2005/2005-summer">2005 Summer</a>.
    ...

Let's proceed:

    >>> manager.getControl('Confirm').click()

We get back to the terms index page:

    >>> print analyze.queryHTML('//h1', manager.contents)[0]
    <h1>
      2005
      (May 1, 2005&#8212;
       Oct 15, 2005)
    </h1>

But the deleted term is not there:

    >>> '2005-summer' not in manager.contents
    True
    >>> '2005 summer' not in manager.contents
    True
