|
3 | 3 | """
|
4 | 4 | import json
|
5 | 5 | from gettext import GNUTranslations
|
| 6 | +from django.test import TestCase |
6 | 7 |
|
7 | 8 | from completion.test_utils import CompletionWaffleTestMixin
|
8 | 9 | from django.db import connections, transaction
|
|
24 | 25 | from openedx.core.djangoapps.dark_lang.models import DarkLangConfig
|
25 | 26 | from openedx.core.djangoapps.xblock import api as xblock_api
|
26 | 27 | from openedx.core.djangolib.testing.utils import skip_unless_lms, skip_unless_cms
|
| 28 | +from openedx.core.lib.xblock_serializer import api as serializer_api |
27 | 29 | from common.djangoapps.student.tests.factories import UserFactory
|
28 | 30 |
|
29 | 31 |
|
@@ -59,6 +61,86 @@ def setUp(self):
|
59 | 61 | )
|
60 | 62 |
|
61 | 63 |
|
| 64 | +@skip_unless_cms |
| 65 | +class ContentLibraryOlxTests(ContentLibraryContentTestMixin, TestCase): |
| 66 | + """ |
| 67 | + Basic test of the Learning-Core-based XBlock serialization-deserialization, using XBlocks in a content library. |
| 68 | + """ |
| 69 | + |
| 70 | + def test_html_round_trip(self): |
| 71 | + """ |
| 72 | + Test that if we deserialize and serialize an HTMLBlock repeatedly, two things hold true: |
| 73 | +
|
| 74 | + 1. Even if the OLX changes format, the inner content does not change format. |
| 75 | + 2. The OLX settles into a stable state after 1 round trip. |
| 76 | +
|
| 77 | + (We are particularly testing HTML, but it would be good to confirm that these principles hold true for |
| 78 | + XBlocks in general.) |
| 79 | + """ |
| 80 | + usage_key = library_api.create_library_block(self.library.key, "html", "roundtrip").usage_key |
| 81 | + |
| 82 | + # The block's actual HTML has some extraneous spaces and newlines, as well as comment. |
| 83 | + # We expect this to be preserved through the round-trips. |
| 84 | + block_content = '''\ |
| 85 | +<div class="i-like-double-quotes"> |
| 86 | + <div class='i-like-single-quotes'> |
| 87 | + <p> There is a space on either side of this sentence. </p> |
| 88 | + <p>\tThere is a tab on either side of this sentence.\t<p> |
| 89 | + <p>🙃There is an emoji on either side of this sentence.🙂</p> |
| 90 | + <p>There is nothing on either side of this sentence.</p> |
| 91 | + </div> |
| 92 | + <p><![CDATA[ This is an inner CDATA 🤯 Technically illegal in HTML, but let's test it anyway. ?!&<>\t ]]></p> |
| 93 | + <!-- This is a comment within the HTML. --> |
| 94 | +</div>''' |
| 95 | + |
| 96 | + # The OLX containing the HTML also has some extraneous stuff, which do *not* expect to survive the round-trip. |
| 97 | + olx_1 = f'''\ |
| 98 | +
|
| 99 | + <html |
| 100 | + display_name="Round Trip Test HTML Block" |
| 101 | + some_fake_field="some fake value" |
| 102 | + ><![CDATA[{block_content}]]><!-- |
| 103 | + I am an OLX comment. |
| 104 | + --></html>''' |
| 105 | + |
| 106 | + # Here is what we expect the OLX to settle down to. Notable changes: |
| 107 | + # * url_name is added. |
| 108 | + # * some_fake_field is gone. |
| 109 | + # * The OLX comment is gone. |
| 110 | + # * A trailing newline is added at the end of the export. |
| 111 | + # DEVS: If you are purposefully tweaking the formatting of the xblock serializer, then it's fine to |
| 112 | + # update the value of this variable, as long as: |
| 113 | + # 1. the {block_content} remains unchanged, and |
| 114 | + # 2. the canonical_olx remains stable through the 2nd round trip. |
| 115 | + canonical_olx = ( |
| 116 | + f'<html url_name="roundtrip" display_name="Round Trip Test HTML Block"><![CDATA[{block_content}]]></html>\n' |
| 117 | + ) |
| 118 | + |
| 119 | + # Save the block to LC, and re-load it. |
| 120 | + library_api.set_library_block_olx(usage_key, olx_1) |
| 121 | + library_api.publish_changes(self.library.key) |
| 122 | + block_saved_1 = xblock_api.load_block(usage_key, self.staff_user) |
| 123 | + |
| 124 | + # Content should be preserved... |
| 125 | + assert block_saved_1.data == block_content |
| 126 | + |
| 127 | + # ...but the serialized OLX will have changed to match the 'canonical' OLX. |
| 128 | + olx_2 = serializer_api.serialize_xblock_to_olx(block_saved_1).olx_str |
| 129 | + assert olx_2 == canonical_olx |
| 130 | + |
| 131 | + # Now, save that OLX back to LC, and re-load it again. |
| 132 | + library_api.set_library_block_olx(usage_key, olx_2) |
| 133 | + library_api.publish_changes(self.library.key) |
| 134 | + block_saved_2 = xblock_api.load_block(usage_key, self.staff_user) |
| 135 | + |
| 136 | + # Again, content should be preserved... |
| 137 | + assert block_saved_2.data == block_saved_1.data == block_content |
| 138 | + |
| 139 | + # ...and this time, the OLX should have settled too. |
| 140 | + olx_3 = serializer_api.serialize_xblock_to_olx(block_saved_2).olx_str |
| 141 | + assert olx_3 == olx_2 == canonical_olx |
| 142 | + |
| 143 | + |
62 | 144 | class ContentLibraryRuntimeTests(ContentLibraryContentTestMixin):
|
63 | 145 | """
|
64 | 146 | Basic tests of the Learning-Core-based XBlock runtime using XBlocks in a
|
|
0 commit comments