def testReplaceText(self):
     from collective.searchandreplace.searchreplaceutility import getRawText
     setRoles(self.portal, TEST_USER_ID, ['Manager'])
     self.portal.invokeFactory('Document', 'doc1')
     doc1 = getattr(self.portal, 'doc1')
     edit_content(doc1,
                  title='Test Title',
                  description='Test Description',
                  text='Test Case')
     self.assertEqual(getRawText(doc1), 'Test Case')
     # Search it.
     parameters = dict(context=doc1,
                       find='test case',
                       replaceText='foo',
                       matchCase=False)
     results = self.srutil.searchObjects(**parameters)
     self.assertEqual(len(results), 1)
     self.assertEqual(getRawText(doc1), 'Test Case')
     # Replace it.
     parameters['doReplace'] = True
     results = self.srutil.searchObjects(**parameters)
     # Note: replacing returns an int, not a list.
     self.assertEqual(results, 1)
     self.assertEqual(getRawText(doc1), 'foo')
     # Other fields are not changed.
     self.assertEqual(doc1.Title(), 'Test Title')
     self.assertEqual(doc1.Description(), 'Test Description')
 def testReplaceText(self):
     from collective.searchandreplace.searchreplaceutility import getRawText
     setRoles(self.portal, TEST_USER_ID, ['Manager'])
     self.portal.invokeFactory('Document', 'doc1')
     doc1 = getattr(self.portal, 'doc1')
     edit_content(
         doc1,
         title='Test Title',
         description='Test Description',
         text='Test Case')
     self.assertEqual(getRawText(doc1), 'Test Case')
     # Search it.
     parameters = dict(
         context=doc1,
         find='test case',
         replaceText='foo',
         matchCase=False)
     results = self.srutil.searchObjects(**parameters)
     self.assertEqual(len(results), 1)
     self.assertEqual(getRawText(doc1), 'Test Case')
     # Replace it.
     parameters['doReplace'] = True
     results = self.srutil.searchObjects(**parameters)
     # Note: replacing returns an int, not a list.
     self.assertEqual(results, 1)
     self.assertEqual(getRawText(doc1), 'foo')
     # Other fields are not changed.
     self.assertEqual(doc1.Title(), 'Test Title')
     self.assertEqual(doc1.Description(), 'Test Description')
    def testReplaceRawHTML(self):
        from collective.searchandreplace.searchreplaceutility import getRawText
        setRoles(self.portal, TEST_USER_ID, ['Manager'])
        self.portal.invokeFactory('Document', 'doc1')
        doc1 = getattr(self.portal, 'doc1')
        edit_content(doc1,
                     title='Test Title',
                     text='My <strong>Test</strong> Case')
        self.assertEqual(getRawText(doc1), 'My <strong>Test</strong> Case')

        # Search only in SearchableText.
        parameters = dict(context=doc1,
                          find='<strong>Test</strong>',
                          replaceText='<em>Test</em>',
                          onlySearchableText=True,
                          matchCase=False)
        results = self.srutil.searchObjects(**parameters)
        self.assertEqual(len(results), 0)

        # Search everywhere.
        parameters['onlySearchableText'] = False
        results = self.srutil.searchObjects(**parameters)
        self.assertEqual(len(results), 1)
        self.assertEqual(getRawText(doc1), 'My <strong>Test</strong> Case')

        # Replace it.
        parameters['doReplace'] = True
        results = self.srutil.searchObjects(**parameters)
        # Note: replacing returns an int, not a list.
        self.assertEqual(results, 1)
        self.assertEqual(getRawText(doc1), 'My <em>Test</em> Case')
Exemplo n.º 4
0
    def testReplaceEmpty(self):
        from collective.searchandreplace.searchreplaceutility import getRawText

        setRoles(self.portal, TEST_USER_ID, ["Manager"])
        self.portal.invokeFactory("Document", "doc1")
        doc1 = getattr(self.portal, "doc1")
        edit_content(
            doc1,
            title="Find My Title",
            description="Find My Description",
            text="Find My Text",
        )
        self.assertEqual(getRawText(doc1), "Find My Text")
        # Search it.
        parameters = dict(context=doc1, findWhat=" My", matchCase=False)
        results = self.srutil.findObjects(**parameters)
        self.assertEqual(len(results), 3)
        self.assertEqual(getRawText(doc1), "Find My Text")
        r_parameters = dict(replaceWith="", )
        r_parameters.update(parameters)
        results = self.srutil.replaceAllMatches(**r_parameters)
        # Note: replacing returns an int, not a list.
        self.assertEqual(results, 3)
        self.assertEqual(getRawText(doc1), "Find Text")
        self.assertEqual(doc1.Title(), "Find Title")
        self.assertEqual(doc1.Description(), "Find Description")
Exemplo n.º 5
0
    def testReplaceRawHTML(self):
        from collective.searchandreplace.searchreplaceutility import getRawText

        setRoles(self.portal, TEST_USER_ID, ["Manager"])
        self.portal.invokeFactory("Document", "doc1")
        doc1 = getattr(self.portal, "doc1")
        edit_content(doc1,
                     title="Find Title",
                     text="My <strong>Test</strong> Case")
        self.assertEqual(getRawText(doc1), "My <strong>Test</strong> Case")

        # Search only in SearchableText.
        parameters = dict(
            context=doc1,
            findWhat="<strong>Test</strong>",
            onlySearchableText=True,
            matchCase=False,
        )
        results = self.srutil.findObjects(**parameters)
        self.assertEqual(len(results), 0)

        # Search everywhere.
        parameters["onlySearchableText"] = False
        results = self.srutil.findObjects(**parameters)
        self.assertEqual(len(results), 1)
        self.assertEqual(getRawText(doc1), "My <strong>Test</strong> Case")

        r_parameters = dict(replaceWith="<em>Test</em>", )
        r_parameters.update(parameters)
        # Replace it.
        results = self.srutil.replaceAllMatches(**r_parameters)
        # Note: replacing returns an int, not a list.
        self.assertEqual(results, 1)
        self.assertEqual(getRawText(doc1), "My <em>Test</em> Case")
    def testReplaceRawHTML(self):
        from collective.searchandreplace.searchreplaceutility import getRawText
        setRoles(self.portal, TEST_USER_ID, ['Manager'])
        self.portal.invokeFactory('Document', 'doc1')
        doc1 = getattr(self.portal, 'doc1')
        edit_content(
            doc1,
            title='Test Title',
            text='My <strong>Test</strong> Case')
        self.assertEqual(getRawText(doc1), 'My <strong>Test</strong> Case')

        # Search only in SearchableText.
        parameters = dict(
            context=doc1,
            find='<strong>Test</strong>',
            replaceText='<em>Test</em>',
            onlySearchableText=True,
            matchCase=False)
        results = self.srutil.searchObjects(**parameters)
        self.assertEqual(len(results), 0)

        # Search everywhere.
        parameters['onlySearchableText'] = False
        results = self.srutil.searchObjects(**parameters)
        self.assertEqual(len(results), 1)
        self.assertEqual(getRawText(doc1), 'My <strong>Test</strong> Case')

        # Replace it.
        parameters['doReplace'] = True
        results = self.srutil.searchObjects(**parameters)
        # Note: replacing returns an int, not a list.
        self.assertEqual(results, 1)
        self.assertEqual(getRawText(doc1), 'My <em>Test</em> Case')
Exemplo n.º 7
0
    def testReplaceUnsearchableTextFields(self):
        from collective.searchandreplace.searchreplaceutility import getRawText

        setRoles(self.portal, TEST_USER_ID, ["Manager"])
        # Note: we use our sample type here, which has extra fields.
        # This currently is Archetypes when testing on Plone 4,
        # and Dexterity when testing on Plone 5.
        self.portal.invokeFactory("SampleType", "doc1")
        doc1 = getattr(self.portal, "doc1")
        edit_content(
            doc1,
            title="Find Title",
            unsearchable="Find Unsearchable",
        )

        # Test the initial values.
        self.assertEqual(doc1.Title(), "Find Title")
        self.assertEqual(getRawText(doc1, "unsearchable"), "Find Unsearchable")

        # Search it with onlySearchableText true.
        parameters = dict(
            context=doc1,
            findWhat="Unsearchable",
            onlySearchableText=True,
            matchCase=False,
        )
        results = self.srutil.findObjects(**parameters)
        self.assertEqual(len(results), 0)

        # Replace it with onlySearchableText true.
        r_parameters = dict(replaceWith="Foo", )
        r_parameters.update(parameters)
        results = self.srutil.replaceAllMatches(**r_parameters)
        self.assertEqual(results, 0)

        # Nothing should have changed.
        self.assertEqual(getRawText(doc1, "unsearchable"), "Find Unsearchable")

        # Now search everything, so onlySearchableText false.
        parameters["onlySearchableText"] = False
        results = self.srutil.findObjects(**parameters)
        self.assertEqual(len(results), 1)

        # Nothing should have changed.
        self.assertEqual(getRawText(doc1, "unsearchable"), "Find Unsearchable")

        # Replace it.
        r_parameters["onlySearchableText"] = False
        results = self.srutil.replaceAllMatches(**r_parameters)
        self.assertEqual(results, 1)
        self.assertEqual(getRawText(doc1, "unsearchable"), "Find Foo")
    def testReplaceUnsearchableTextFields(self):
        from collective.searchandreplace.searchreplaceutility import getRawText
        setRoles(self.portal, TEST_USER_ID, ['Manager'])
        # Note: we use our sample type here, which has extra fields.
        # This currently is Archetypes when testing on Plone 4,
        # and Dexterity when testing on Plone 5.
        self.portal.invokeFactory('SampleType', 'doc1')
        doc1 = getattr(self.portal, 'doc1')
        edit_content(
            doc1,
            title='Test Title',
            unsearchable='Test Unsearchable',
        )

        # Test the initial values.
        self.assertEqual(doc1.Title(), 'Test Title')
        self.assertEqual(getRawText(doc1, 'unsearchable'), 'Test Unsearchable')

        # Search it with onlySearchableText true.
        parameters = dict(
            context=doc1,
            find='Unsearchable',
            replaceText='Foo',
            onlySearchableText=True,
            matchCase=False)
        results = self.srutil.searchObjects(**parameters)
        self.assertEqual(len(results), 0)

        # Replace it with onlySearchableText true.
        parameters['doReplace'] = True
        results = self.srutil.searchObjects(**parameters)
        self.assertEqual(results, 0)

        # Nothing should have changed.
        self.assertEqual(getRawText(doc1, 'unsearchable'), 'Test Unsearchable')

        # Now search everything, so onlySearchableText false.
        parameters['onlySearchableText'] = False
        parameters['doReplace'] = False
        results = self.srutil.searchObjects(**parameters)
        self.assertEqual(len(results), 1)

        # Nothing should have changed.
        self.assertEqual(getRawText(doc1, 'unsearchable'), 'Test Unsearchable')

        # Replace it.
        parameters['doReplace'] = True
        results = self.srutil.searchObjects(**parameters)
        self.assertEqual(results, 1)
        self.assertEqual(getRawText(doc1, 'unsearchable'), 'Test Foo')
    def testReplaceUnsearchableTextFields(self):
        from collective.searchandreplace.searchreplaceutility import getRawText
        setRoles(self.portal, TEST_USER_ID, ['Manager'])
        # Note: we use our sample type here, which has extra fields.
        # This currently is Archetypes when testing on Plone 4,
        # and Dexterity when testing on Plone 5.
        self.portal.invokeFactory('SampleType', 'doc1')
        doc1 = getattr(self.portal, 'doc1')
        edit_content(
            doc1,
            title='Test Title',
            unsearchable='Test Unsearchable',
        )

        # Test the initial values.
        self.assertEqual(doc1.Title(), 'Test Title')
        self.assertEqual(getRawText(doc1, 'unsearchable'), 'Test Unsearchable')

        # Search it with onlySearchableText true.
        parameters = dict(context=doc1,
                          find='Unsearchable',
                          replaceText='Foo',
                          onlySearchableText=True,
                          matchCase=False)
        results = self.srutil.searchObjects(**parameters)
        self.assertEqual(len(results), 0)

        # Replace it with onlySearchableText true.
        parameters['doReplace'] = True
        results = self.srutil.searchObjects(**parameters)
        self.assertEqual(results, 0)

        # Nothing should have changed.
        self.assertEqual(getRawText(doc1, 'unsearchable'), 'Test Unsearchable')

        # Now search everything, so onlySearchableText false.
        parameters['onlySearchableText'] = False
        parameters['doReplace'] = False
        results = self.srutil.searchObjects(**parameters)
        self.assertEqual(len(results), 1)

        # Nothing should have changed.
        self.assertEqual(getRawText(doc1, 'unsearchable'), 'Test Unsearchable')

        # Replace it.
        parameters['doReplace'] = True
        results = self.srutil.searchObjects(**parameters)
        self.assertEqual(results, 1)
        self.assertEqual(getRawText(doc1, 'unsearchable'), 'Test Foo')
Exemplo n.º 10
0
    def testReplaceDescription(self):
        from collective.searchandreplace.searchreplaceutility import getRawText

        setRoles(self.portal, TEST_USER_ID, ["Manager"])
        self.portal.invokeFactory("Document", "doc1")
        doc1 = getattr(self.portal, "doc1")
        edit_content(doc1,
                     title="Find Title",
                     description="Find Description",
                     text="Find Case")
        self.assertEqual(doc1.Description(), "Find Description")
        # Search it.
        parameters = dict(context=doc1, findWhat="find desc", matchCase=False)
        results = self.srutil.findObjects(**parameters)
        self.assertEqual(len(results), 1)
        self.assertEqual(doc1.Description(), "Find Description")

        r_parameters = dict(replaceWith="replaced", )
        r_parameters.update(parameters)
        results = self.srutil.replaceAllMatches(**r_parameters)
        self.assertEqual(results, 1)
        # Note: we have replaced only part of the description.
        self.assertEqual(doc1.Description(), "replacedription")

        # Other fields are not changed.
        self.assertEqual(doc1.Title(), "Find Title")
        self.assertEqual(getRawText(doc1), "Find Case")
Exemplo n.º 11
0
    def replace(self):
        from collective.searchandreplace.searchreplaceutility import getRawText

        doc1 = getattr(self.portal, "doc1")
        self.assertEqual(getRawText(doc1), "Find Case")
        # Replace it after 1 second to ensure different modified date
        import time

        time.sleep(1)
        replace_parameters = dict(context=doc1,
                                  findWhat="find case",
                                  replaceWith="replaced",
                                  matchCase=False)
        results = self.srutil.replaceAllMatches(**replace_parameters)
        self.assertEqual(results, 1)
        self.assertEqual(getRawText(doc1), "replaced")
Exemplo n.º 12
0
    def testReplaceAll(self):
        from collective.searchandreplace.searchreplaceutility import getRawText

        results = self.srutil.replaceAllMatches(
            self.portal,
            "Find",
            replaceWith="Bike",
            matchCase=True,
        )
        self.assertEqual(results, 4)
        self.assertEqual(self.doc1.Title(), "Bike find Bike find")
        self.assertEqual(getRawText(self.doc1), "Bike find\nBike find")
Exemplo n.º 13
0
 def testReplaceAll(self):
     from collective.searchandreplace.searchreplaceutility import getRawText
     results = self.srutil.searchObjects(
         self.portal,
         'Test',
         replaceText='Bike',
         matchCase=True,
         doReplace=True,
     )
     self.assertEqual(results, 4)
     self.assertEqual(self.doc1.Title(), 'Bike test Bike test')
     self.assertEqual(getRawText(self.doc1), 'Bike test\nBike test')
Exemplo n.º 14
0
    def testReplacePaths(self):
        from collective.searchandreplace.searchreplaceutility import getRawText

        path = "/".join(self.doc1.getPhysicalPath())
        paths = {
            path: {
                # Replace two positions from title
                "title": [0, 10],
                # Replace two positions from text
                "text": [5, 15],
            }
        }
        results = self.srutil.replaceFilteredOccurences(
            self.portal,
            "Find",
            replaceWith="Bike",
            occurences=paths,
            matchCase=False,
        )
        self.assertEqual(results, 4)
        self.assertEqual(self.doc1.Title(), "Bike find Bike find")
        self.assertEqual(getRawText(self.doc1), "Find Bike\nFind Bike")
Exemplo n.º 15
0
 def testReplacePaths(self):
     from collective.searchandreplace.searchreplaceutility import getRawText
     path = '/'.join(self.doc1.getPhysicalPath())
     paths = {
         path: {
             # Replace two positions from title
             'title': [0, 10],
             # Replace two positions from text
             'text': [5, 15],
         }
     }
     results = self.srutil.searchObjects(
         self.portal,
         'Test',
         replaceText='Bike',
         matchCase=False,
         doReplace=True,
         searchItems=paths,
     )
     self.assertEqual(results, 4)
     self.assertEqual(self.doc1.Title(), 'Bike test Bike test')
     self.assertEqual(getRawText(self.doc1), 'Test Bike\nTest Bike')
    def testReplaceAllTextFields(self):
        from collective.searchandreplace.searchreplaceutility import getRawText
        setRoles(self.portal, TEST_USER_ID, ['Manager'])
        # Note: we use our sample type here, which has extra fields.
        # This currently is Archetypes when testing on Plone 4,
        # and Dexterity when testing on Plone 5.
        self.portal.invokeFactory('SampleType', 'doc1')
        doc1 = getattr(self.portal, 'doc1')
        edit_content(
            doc1,
            title='Test Title',
            description='Test Description',
            rich='Test Rich',
            plain='Test Plain',
            line='Test Line',
            unsearchable='Test Unsearchable',
        )

        # Test the initial values.
        self.assertEqual(doc1.Title(), 'Test Title')
        self.assertEqual(doc1.Description(), 'Test Description')
        self.assertEqual(getRawText(doc1, 'rich'), 'Test Rich')
        self.assertEqual(getRawText(doc1, 'plain'), 'Test Plain')
        self.assertEqual(getRawText(doc1, 'line'), 'Test Line')
        self.assertEqual(getRawText(doc1, 'unsearchable'), 'Test Unsearchable')

        # Search it.
        parameters = dict(
            context=doc1,
            find='Test',
            replaceText='Foo',
            matchCase=False)
        results = self.srutil.searchObjects(**parameters)
        self.assertEqual(len(results), 5)

        # Nothing should have changed.
        self.assertEqual(doc1.Title(), 'Test Title')
        self.assertEqual(doc1.Description(), 'Test Description')
        self.assertEqual(getRawText(doc1, 'rich'), 'Test Rich')
        self.assertEqual(getRawText(doc1, 'plain'), 'Test Plain')
        self.assertEqual(getRawText(doc1, 'line'), 'Test Line')
        self.assertEqual(getRawText(doc1, 'unsearchable'), 'Test Unsearchable')

        # Replace it.
        parameters['doReplace'] = True
        results = self.srutil.searchObjects(**parameters)
        self.assertEqual(results, 5)

        # Most fields should have changed.
        self.assertEqual(doc1.Title(), 'Foo Title')
        self.assertEqual(doc1.Description(), 'Foo Description')
        self.assertEqual(getRawText(doc1, 'rich'), 'Foo Rich')
        self.assertEqual(getRawText(doc1, 'plain'), 'Foo Plain')
        self.assertEqual(getRawText(doc1, 'unsearchable'), 'Foo Unsearchable')

        # But not the textline field.
        self.assertEqual(getRawText(doc1, 'line'), 'Test Line')
Exemplo n.º 17
0
    def testReplaceAllFields(self):
        from collective.searchandreplace.searchreplaceutility import getRawText

        setRoles(self.portal, TEST_USER_ID, ["Manager"])
        # Note: we use our sample type here, which has extra fields.
        # This currently is Archetypes when testing on Plone 4,
        # and Dexterity when testing on Plone 5.
        self.portal.invokeFactory("SampleType", "doc1")
        doc1 = getattr(self.portal, "doc1")
        edit_content(
            doc1,
            title="Find Title",
            description="Find Description",
            rich="Find Rich",
            plain="Find Plain",
            line="Find Line",
            unsearchable="Find Unsearchable",
        )

        # Test the initial values.
        self.assertEqual(doc1.Title(), "Find Title")
        self.assertEqual(doc1.Description(), "Find Description")
        self.assertEqual(getRawText(doc1, "rich"), "Find Rich")
        self.assertEqual(getRawText(doc1, "plain"), "Find Plain")
        self.assertEqual(getRawText(doc1, "line"), "Find Line")
        self.assertEqual(getRawText(doc1, "unsearchable"), "Find Unsearchable")

        # Search it.
        parameters = dict(context=doc1, findWhat="Find", matchCase=False)
        results = self.srutil.findObjects(**parameters)
        self.assertEqual(len(results), 6)

        # Nothing should have changed.
        self.assertEqual(doc1.Title(), "Find Title")
        self.assertEqual(doc1.Description(), "Find Description")
        self.assertEqual(getRawText(doc1, "rich"), "Find Rich")
        self.assertEqual(getRawText(doc1, "plain"), "Find Plain")
        self.assertEqual(getRawText(doc1, "line"), "Find Line")
        self.assertEqual(getRawText(doc1, "unsearchable"), "Find Unsearchable")

        r_parameters = dict(replaceWith="Foo", )
        r_parameters.update(parameters)
        results = self.srutil.replaceAllMatches(**r_parameters)
        self.assertEqual(results, 6)

        # All fields should have changed.
        self.assertEqual(doc1.Title(), "Foo Title")
        self.assertEqual(doc1.Description(), "Foo Description")
        self.assertEqual(getRawText(doc1, "rich"), "Foo Rich")
        self.assertEqual(getRawText(doc1, "plain"), "Foo Plain")
        self.assertEqual(getRawText(doc1, "unsearchable"), "Foo Unsearchable")

        # including the textline field.
        self.assertEqual(getRawText(doc1, "line"), "Foo Line")
    def testReplaceAllTextFields(self):
        from collective.searchandreplace.searchreplaceutility import getRawText
        setRoles(self.portal, TEST_USER_ID, ['Manager'])
        # Note: we use our sample type here, which has extra fields.
        # This currently is Archetypes when testing on Plone 4,
        # and Dexterity when testing on Plone 5.
        self.portal.invokeFactory('SampleType', 'doc1')
        doc1 = getattr(self.portal, 'doc1')
        edit_content(
            doc1,
            title='Test Title',
            description='Test Description',
            rich='Test Rich',
            plain='Test Plain',
            line='Test Line',
            unsearchable='Test Unsearchable',
        )

        # Test the initial values.
        self.assertEqual(doc1.Title(), 'Test Title')
        self.assertEqual(doc1.Description(), 'Test Description')
        self.assertEqual(getRawText(doc1, 'rich'), 'Test Rich')
        self.assertEqual(getRawText(doc1, 'plain'), 'Test Plain')
        self.assertEqual(getRawText(doc1, 'line'), 'Test Line')
        self.assertEqual(getRawText(doc1, 'unsearchable'), 'Test Unsearchable')

        # Search it.
        parameters = dict(context=doc1,
                          find='Test',
                          replaceText='Foo',
                          matchCase=False)
        results = self.srutil.searchObjects(**parameters)
        self.assertEqual(len(results), 5)

        # Nothing should have changed.
        self.assertEqual(doc1.Title(), 'Test Title')
        self.assertEqual(doc1.Description(), 'Test Description')
        self.assertEqual(getRawText(doc1, 'rich'), 'Test Rich')
        self.assertEqual(getRawText(doc1, 'plain'), 'Test Plain')
        self.assertEqual(getRawText(doc1, 'line'), 'Test Line')
        self.assertEqual(getRawText(doc1, 'unsearchable'), 'Test Unsearchable')

        # Replace it.
        parameters['doReplace'] = True
        results = self.srutil.searchObjects(**parameters)
        self.assertEqual(results, 5)

        # Most fields should have changed.
        self.assertEqual(doc1.Title(), 'Foo Title')
        self.assertEqual(doc1.Description(), 'Foo Description')
        self.assertEqual(getRawText(doc1, 'rich'), 'Foo Rich')
        self.assertEqual(getRawText(doc1, 'plain'), 'Foo Plain')
        self.assertEqual(getRawText(doc1, 'unsearchable'), 'Foo Unsearchable')

        # But not the textline field.
        self.assertEqual(getRawText(doc1, 'line'), 'Test Line')