@@ -826,3 +826,73 @@ def test_author_view(self):
826826 assert '<li>html 2</li>' in rendered .content
827827 assert '<li>html 3</li>' in rendered .content
828828 assert '<li>html 4</li>' in rendered .content
829+
830+
831+ class TestLegacyLibraryContentBlockMigrationPublishing (LegacyLibraryContentTest ):
832+ """
833+ Unit tests for the `publish_if_was_published` flag of
834+ LegacyLibraryContentBlock.v2_update_children_upstream_version.
835+ """
836+
837+ def setUp (self ):
838+ from cms .djangoapps .modulestore_migrator import api
839+ from cms .djangoapps .modulestore_migrator .data import CompositionLevel , RepeatHandlingStrategy
840+ super ().setUp ()
841+ user = UserFactory ()
842+ self ._sync_lc_block_from_library ()
843+ self .organization = OrganizationFactory (short_name = "myorg" )
844+ lib_api .create_library (
845+ org = self .organization ,
846+ slug = "mylib" ,
847+ title = "My Test V2 Library" ,
848+ )
849+ self .library_v2 = lib_api .ContentLibrary .objects .get (slug = "mylib" )
850+ api .start_migration_to_library (
851+ user = user ,
852+ source_key = self .library .location .library_key ,
853+ target_library_key = self .library_v2 .library_key ,
854+ target_collection_slug = None ,
855+ composition_level = CompositionLevel .Component ,
856+ repeat_handling_strategy = RepeatHandlingStrategy .Skip ,
857+ preserve_url_slugs = True ,
858+ forward_source_to_target = True ,
859+ )
860+
861+ def test_publishes_block_that_was_previously_published (self ):
862+ """
863+ If the LC block was published before migration, and `publish_if_was_published=True`,
864+ the migration should re-publish it so the new upstream links reach the published branch.
865+ """
866+ self .store .publish (self .course .location , self .user_id )
867+
868+ self .lc_block .v2_update_children_upstream_version (self .user_id , publish_if_was_published = True )
869+
870+ with self .store .branch_setting (ModuleStoreEnum .Branch .published_only ):
871+ published_block = self .store .get_item (self .lc_block .location )
872+ assert published_block .is_migrated_to_v2 is True
873+ assert published_block .get_children ()[0 ].upstream == "lb:myorg:mylib:html:html_1"
874+
875+ def test_does_not_publish_when_flag_is_false (self ):
876+ """
877+ Even if the block was published before migration, it should NOT be re-published
878+ when `publish_if_was_published` is False (the default).
879+ """
880+ self .store .publish (self .course .location , self .user_id )
881+
882+ self .lc_block .v2_update_children_upstream_version (self .user_id )
883+
884+ with self .store .branch_setting (ModuleStoreEnum .Branch .published_only ):
885+ published_block = self .store .get_item (self .lc_block .location )
886+ # The published version still reflects the pre-migration state.
887+ assert published_block .is_migrated_to_v2 is False
888+
889+ def test_does_not_publish_block_that_was_never_published (self ):
890+ """
891+ If the LC block was never published, `publish_if_was_published=True` should not
892+ cause it to become published.
893+ """
894+ assert not self .store .has_published_version (self .lc_block )
895+
896+ self .lc_block .v2_update_children_upstream_version (self .user_id , publish_if_was_published = True )
897+
898+ assert not self .store .has_published_version (self .store .get_item (self .lc_block .location ))
0 commit comments