Alex Willmer avatar Alex Willmer committed 7469e38

Fix link field population when using Ticket.populate_from(), correct tests of same

Comments (0)

Files changed (4)

trac/ticket/api.py

            return []
        return list(unique(int(id) for id in self.NUMBERS_RE.findall(value)))
 
+    def other_end(self, end):
+        """Return the other end of a bidirectional link, end if the link is
+        unidirectional.
+        """
+        other_end = self.link_ends_map[end]
+        if other_end is None:
+            return end
+        else:
+            return other_end
+
     # IResourceManager methods
 
     def get_resource_realms(self):

trac/ticket/model.py

             link_field = [f for f in self.fields 
                             if f['name'] == link_field_name][0]
             copy_field_names = link_field['copy_fields']
-            self[link_field_name] = str('#%s' % tkt_id)
-        
+            
+            # Record in this ticket (self) the ticket id from which we have 
+            # populated the values, against the other end of the link.
+            # Doing so will cause this our ticket (self.id) to be recorded in 
+            # link_field_name on the other ticket, if link_field_name is
+            # bi-directional ticket link
+            other_end = ticket_sys.other_end(link_field_name)
+            self[other_end] = str('#%s' % tkt_id)
+            
         try:
             tkt_id = int(tkt_id)
         except ValueError:

trac/ticket/tests/links.py

 class TicketTestCase(unittest.TestCase):
     def setUp(self):
         self.env = EnvironmentStub(default_data=True)
+        self.env.config.set('ticket', 'default_copy_fields', 'summary')
+        self.env.config.set('ticket-custom', 'foo', 'text')
         self.env.config.set('ticket-links', 'dependency', 'dependson,dependent')
         self.env.config.set('ticket-links', 'dependency.validator', 'no_cycle')
+        self.env.config.set('ticket-links', 'dependent.copy_fields', '')
         self.env.config.set('ticket-links', 'parent_children', 
                                                             'parent,children')
         self.env.config.set('ticket-links', 'parent_children.validator',
                                                             'parent_child')
         self.env.config.set('ticket-links', 'children.blocks', 'true')
         self.env.config.set('ticket-links', 'children.label', 'Overridden')
-        self.env.config.set('ticket-links', 'parent.copy_fields',
+        self.env.config.set('ticket-links', 'children.copy_fields',
                                                             'summary, foo')
         self.env.config.set('ticket-links', 'oneway', 'refersto')
         self.req = Mock(href=self.env.href, authname='anonymous', tz=utc,
         self.assertFalse(links_provider.is_blocker('parent'))
         self.assertTrue(links_provider.is_blocker('children'))
         
+    def test_get_copy_fields(self):
+        links_provider = LinksProvider(self.env)
+        self.assertEquals([],
+                          links_provider.get_copy_fields('dependent'))
+        self.assertEquals(['summary', 'foo'], 
+                          links_provider.get_copy_fields('children'))
+        self.assertEquals(['summary'], 
+                          links_provider.get_copy_fields('refersto'))
+
     def test_link_ends_map(self):
         ticket_system = TicketSystem(self.env)
         self.assertEquals(ticket_system.link_ends_map,
                            'parent': 'children', 'children': 'parent',
                            'refersto': None})
     
+    def test_other_end(self):
+        ticket_system = TicketSystem(self.env)
+        self.assertEquals('dependent', ticket_system.other_end('dependson'))
+        self.assertEquals('children',  ticket_system.other_end('parent'))
+        self.assertEquals('refersto',  ticket_system.other_end('refersto'))
+        self.assertEquals('dependent', ticket_system.other_end('dependson'))
+
     def test_parse_links(self):
         ticket_system = TicketSystem(self.env)
         self.assertEquals([1, 2, 42], ticket_system.parse_links('1 2 42'))
         ticket = Ticket(self.env)
         ticket.populate_from(1, link_field_name='children')
         self.assertEqual('Foo', ticket['summary'])
-        self.assertEqual('#1', ticket['children'])
+        self.assertEqual('This is a custom field', ticket['foo'])
+        self.assertEqual('#1', ticket['parent'])
         
     def test_save_retrieve_links(self):
         ticket = self._create_a_ticket()

trac/ticket/web_ui.py

 
         return 'ticket.html', data, None
 
-    def _other_end(self, field, link_ends_map):
-        other_end = link_ends_map[field['name']]
-        if other_end is None:
-            return field['name']
-        return  other_end
-        
     def _prepare_data(self, req, ticket, absurls=False):
         ticket_sys = TicketSystem(self.env)
-        link_ends_map = ticket_sys.link_ends_map
         context = Context.from_request(req, ticket.resource, absurls=absurls)
         link_fields = [field for field in ticket.fields 
                              if field['type'] == 'link']
         # Create a pairwise list of (label, end_on_the_other_ticket)
         # A ticket created using a given label will then be linked to this
         # ticket by the chosen end
-        new_linked = [(field['label'], self._other_end(field, link_ends_map))
+        new_linked = [(field['label'], ticket_sys.other_end(field['name']))
                       for field in link_fields]
         
         linked_tickets = []
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.