from django.test import RequestFactory from graphene.test import Client from graphql_relay import to_global_id, from_global_id from api.schema import schema from api.utils import get_object from books.factories import ModuleFactory, ChapterFactory, ContentBlockFactory from books.management.commands.migrate_objective_snapshots import migrate_snapshots from books.management.commands.migrate_objectives_to_content import migrate_objectives_to_content from books.models import Snapshot, ChapterSnapshot, ContentBlock from books.tests.queries import MODULE_QUERY, SNAPSHOT_MODULE_QUERY, CREATE_SNAPSHOT_MUTATION, APPLY_SNAPSHOT_MUTATION, \ MODULE_SNAPSHOTS_QUERY, SHARE_SNAPSHOT_MUTATION, UPDATE_SNAPSHOT_MUTATION, DELETE_SNAPSHOT_MUTATION from core.tests.base_test import SkillboxTestCase from objectives.factories import ObjectiveGroupFactory, ObjectiveFactory from users.factories import SchoolClassFactory from users.models import User, SchoolClass class TestSnapshotMigration(SkillboxTestCase): @property def graphene_client(self): return self.get_client() def setUp(self): self.createDefault() self.client = self.get_client() # teacher will create snapshot self.slug = 'some-module' self.module = ModuleFactory(slug=self.slug) self.skillbox_class = SchoolClass.objects.get(name='skillbox') self.teacher2 = User.objects.get(username='teacher2') self.second_class_name = 'second_class' self.second_class = SchoolClass.objects.get(name=self.second_class_name) self.admin = User.objects.get(username='admin') # we make a snapshot S of the module M # snapshot S looks like module M for school class X # module M has a chapter self.chapter = ChapterFactory(parent=self.module, slug='some-chapter', owner=self.admin) ChapterFactory(parent=self.module, slug='some-other-chapter', owner=self.admin) objective_group = ObjectiveGroupFactory(module=self.module, title='Gesellschaft') second_objective_group = ObjectiveGroupFactory(module=self.module, title='Sprache & Kommunikation') self.visible_objective = ObjectiveFactory(text='visible-objective', group=objective_group) self.visible_objective_2 = ObjectiveFactory(text='hidden-objective', group=objective_group) self.custom_objective = ObjectiveFactory(text='custom-objective', group=objective_group, owner=self.teacher) self.custom_hidden_objective = ObjectiveFactory(text='custom-hidden-objective', group=objective_group, owner=self.teacher) self.visible_objective = ObjectiveFactory(text='objective1', group=second_objective_group) self.custom_objective.visible_for.add(self.skillbox_class) self.custom_objective.save() second_objective_group.hidden_for.add(self.skillbox_class) second_objective_group.save() self.custom_hidden_objective.visible_for.remove(self.skillbox_class) self.snapshot1 = Snapshot.objects.create_snapshot(self.module, self.skillbox_class, self.teacher) migrate_objectives_to_content() migrate_snapshots() # Change visibility of objectives resp. content blocks, hide all for content_block in ContentBlock.objects.all().descendant_of(self.chapter): if content_block.owner is None: content_block.hidden_for.add(self.skillbox_class) else: content_block.visible_for.remove(self.skillbox_class) content_block.save() def test_snapshot_migration_dfault_content_pre_apply(self): result = self.client.execute(MODULE_QUERY, variables={ 'slug': self.module.slug }) module = result.data['module'] chapter1 = module['chapters'][0] default_content, _, _ = chapter1['contentBlocks'] # default content block (Verlagsinhalte) exists but is hidden (since one objective is hidden for this class) self.assertEqual(default_content['title'], 'Gesellschaft') self.assertEqual(default_content['originalCreator'], None) self.assertEqual(default_content['contents'][0]['value']['text'], '') self.assertEqual(default_content['hiddenFor'], [{'name': 'skillbox'}]) def test_snapshot_migration_hidden_default_content_creates_custom_content_pre_apply(self): result = self.client.execute(MODULE_QUERY, variables={ 'slug': self.module.slug }) module = result.data['module'] chapter1 = module['chapters'][0] _, custom, _ = chapter1['contentBlocks'] self.assertEqual(custom['title'], 'Gesellschaft') self.assertTrue(custom['originalCreator'] is not None) self.assertEqual(custom['hiddenFor'], []) self.assertEqual(custom['visibleFor'], []) self.assertEqual(custom['contents'][0]['value']['text'], '') def test_snapshot_migration_hidden_content_block_pre_apply(self): result = self.client.execute(MODULE_QUERY, variables={ 'slug': self.module.slug }) module = result.data['module'] chapter1 = module['chapters'][0] _, _, hidden_custom = chapter1['contentBlocks'] # default content block (Verlagsinhalte) exists but is hidden (since one objective is hidden for this class) self.assertEqual(hidden_custom['title'], 'Sprache & Kommunikation') self.assertTrue(hidden_custom['originalCreator'] is None) self.assertEqual(hidden_custom['hiddenFor'], [{'name': 'skillbox'}]) self.assertEqual(hidden_custom['visibleFor'], []) self.assertEqual(hidden_custom['contents'][0]['value']['text'], '') def test_snapshot_migration_default_apply_snapshot(self): # apply snapshot self.snapshot1.apply(self.teacher, self.school_class) result = self.client.execute(MODULE_QUERY, variables={ 'slug': self.module.slug }) module = result.data['module'] chapter1 = module['chapters'][0] default_content, _, _, _ = chapter1['contentBlocks'] # default content block (Verlagsinhalte) exists but is hidden (since one objective is hidden for this class) self.assertEqual(default_content['title'], 'Gesellschaft') self.assertFalse(default_content['userCreated']) self.assertEqual(default_content['originalCreator'], None) self.assertEqual(default_content['contents'][0]['value']['text'], '') self.assertEqual(default_content['hiddenFor'], [{'name': 'skillbox'}]) def test_snapshot_migration_hidden_custom_content_apply_snapshot(self): # custom content from bevore the snapshot must be hidden (visible for nobody) self.snapshot1.apply(self.teacher, self.school_class) result = self.client.execute(MODULE_QUERY, variables={ 'slug': self.module.slug }) module = result.data['module'] chapter1 = module['chapters'][0] _, custom, _, _ = chapter1['contentBlocks'] self.assertEqual(custom['title'], 'Gesellschaft') self.assertTrue(custom['userCreated']) self.assertTrue(custom['originalCreator'] is not None) self.assertEqual(custom['hiddenFor'], []) self.assertEqual(custom['visibleFor'], []) self.assertEqual(custom['contents'][0]['value']['text'], '') def test_snapshot_migration_hidden_content_block_apply_snapshot_2(self): # custom content from bevore the snapshot must be hidden (visible for nobody) self.snapshot1.apply(self.teacher, self.school_class) result = self.client.execute(MODULE_QUERY, variables={ 'slug': self.module.slug }) module = result.data['module'] chapter1 = module['chapters'][0] _, _, hidden_custom, _ = chapter1['contentBlocks'] # default content block (Verlagsinhalte) exists but is hidden (since one objective is hidden for this class) self.assertEqual(hidden_custom['title'], 'Sprache & Kommunikation') self.assertFalse(hidden_custom['userCreated']) self.assertTrue(hidden_custom['originalCreator'] is None) self.assertEqual(hidden_custom['hiddenFor'], [{'name': 'skillbox'}]) self.assertEqual(hidden_custom['visibleFor'], []) self.assertEqual(hidden_custom['contents'][0]['value']['text'], '') def test_snapshot_migration_visible_content_snapshot_new_content(self): # the applicaiton of a snapshot hides old custom content, and creates new custom content for the visible stuff self.snapshot1.apply(self.teacher, self.school_class) result = self.client.execute(MODULE_QUERY, variables={ 'slug': self.module.slug }) module = result.data['module'] chapter1 = module['chapters'][0] _, _, _, new_content_block = chapter1['contentBlocks'] self.assertEqual(new_content_block['title'], 'Gesellschaft') self.assertTrue(new_content_block['userCreated']) self.assertTrue(new_content_block['originalCreator'] is None) self.assertEqual(new_content_block['hiddenFor'], []) self.assertEqual(new_content_block['visibleFor'], [{'name': 'skillbox'}]) self.assertEqual(new_content_block['contents'][0]['value']['text'], '')