'Test that course updates doesn\'t break on old data (content in \'data\' field).
Note: new data will save as list in \'items\' field.'

def test_course_updates_compatibility(self):

    location = self.course.id.make_usage_key('course_info', 'updates')
    course_updates = modulestore().create_item(self.user.id, location.course_key, location.block_type, block_id=location.block_id)
    update_date = u'January    23,    2014'
    update_content = u'Hello    world!'
    update_data = ((((u'<ol><li><h2>' + update_date) + '</h2>') + update_content) + '</li></ol>')
    course_updates.data = update_data
    modulestore().update_item(course_updates, self.user.id)
    course_update_url = self.create_update_url()
    resp = self.client.get_json(course_update_url)
    payload = json.loads(resp.content)
    self.assertEqual(payload, [{u'date': update_date, u'content': update_content, u'id': 1}])
    self.assertEqual(len(payload), 1)
    first_update_url = self.create_update_url(provided_id=payload[0]['id'])
    resp = self.client.get_json(first_update_url)
    payload = json.loads(resp.content)
    self.assertEqual(payload, {u'date': u'January    23,    2014', u'content': u'Hello    world!', u'id': 1})
    self.assertHTMLEqual(update_date, payload['date'])
    self.assertHTMLEqual(update_content, payload['content'])
    course_updates = modulestore().get_item(location)
    self.assertEqual(course_updates.items, [])
    update_content = 'Testing'
    payload = {'content': update_content, 'date': update_date}
    resp = self.client.ajax_post((course_update_url + '1'), payload, HTTP_X_HTTP_METHOD_OVERRIDE='PUT', REQUEST_METHOD='POST')
    self.assertHTMLEqual(update_content, json.loads(resp.content)['content'])
    course_updates = modulestore().get_item(location)
    self.assertEqual(course_updates.items, [{u'date': update_date, u'content': update_content, u'id': 1}])
    self.assertEqual(course_updates.data, '')
    course_updates = modulestore().get_item(location)
    self.assertEqual(course_updates.items, [{u'date': update_date, u'content': update_content, u'id': 1}])
    resp = self.client.delete((course_update_url + '1'))
    self.assertEqual(json.loads(resp.content), [])
    course_updates = modulestore().get_item(location)
    self.assertEqual(course_updates.items, [{u'date': update_date, u'content': update_content, u'id': 1, u'status': 'deleted'}])
    resp = self.client.get_json((course_update_url + '1'))
    payload = json.loads(resp.content)
    self.assertEqual(payload.get('error'), u'Course    update    not    found.')
    self.assertEqual(resp.status_code, 404)
    update_content = u'&lt;problem>\n                                                                                                            &lt;p>&lt;/p>\n                                                                                                            &lt;multiplechoiceresponse>\n                                                                                                            <pre>&lt;problem>\n                                                                                                                            &lt;p>&lt;/p></pre>\n                                                                                                            <div><foo>bar</foo></div>'
    payload = {'content': update_content, 'date': update_date}
    resp = self.client.ajax_post(course_update_url, payload, REQUEST_METHOD='POST')
    self.assertHTMLEqual(update_content, json.loads(resp.content)['content'])
