Commit b7f19f59 authored by Denis Laxalde's avatar Denis Laxalde
Browse files

Delete "container" relation on archive unit when unlinked from a profile

- Container machinery got introduced in 143ae7a4a964, at that time the
  "container" relation was mandatory (on subject) for all entity types.
- Integrity of this relation relied on the assumption that the relation
  was mandatory. (I.e. all entities mush have a link to their container
  and the only way to remove this link is to remove the container entity
- In a88deb387b2b, this assumption got broken as "container" relation
  was made optional for SEDAArchiveUnit as a subject. From there, when
  an archive unit got unlinked from a profile (through deletion of the
  "seda_archive_unit" relation), a "container" relation remained set.
- This is problem since permission (especially on "delete" action)
  relies on the presence or absence of this relation. For instance,
  one would get an error when trying to delete an archive unit they just
  unlinked from a profile because they have no rights to delete the

So we fix this by dropping the "container" relation when a
"seda_archive_unit" relation is deleted through a new hook.

Additional test goes in as other container-related
tests live there.

In migration, we drop spurious "container" relation (not sure there are
parent 2c785ecccbbe
......@@ -90,6 +90,23 @@ class SetContainerHook(hook.Hook):
SetContainerOp.get_instance(self._cw).add_data((self.eidto, self.eidfrom))
class DeleteArchiveUnitContainerHook(hook.Hook):
"""Delete the "container" relation on SEDAArchiveUnit upon deletion of
"seda_archive_unit" relation.
Normally, deletion of "container" relation occurs as a consequence of the
container entity deletion. But for SEDAArchiveUnit, the relation is
optional so deleting the container entity does not delete the relation.
__regid__ = 'seda.archive-unit.delete-container'
__select__ = hook.Hook.__select__ & hook.match_rtype('seda_archive_unit')
events = ('before_delete_relation',)
def __call__(self):
self._cw.execute('DELETE X container Y WHERE X eid %(x)s',
{'x': self.eidfrom})
class CheckRefNonRuleIdCodeListHook(hook.Hook):
"""Watch for addition of concept through seda_ref_non_rule_id_to relation, to ensure it belongs
to the scheme specified on the transfer
......@@ -70,6 +70,24 @@ class ContainerTC(CubicWebTC):
self.assertEqual(entity.cw_adapt_to('IContained').container.eid, transfer.eid)
def test_container_relation_archive_unit(self):
"""Ensure that container relation is deleted upon deletion on
"seda_archive_unit" relation.
with self.admin_access.cnx() as cnx:
transfer = cnx.create_entity('SEDAArchiveTransfer', title=u'test')
unit, _, _ = create_archive_unit(transfer, cnx=cnx,
self.assertEqual(unit.container, (transfer, ))
self.assertIn(unit, transfer.reverse_seda_archive_unit)
self.assertEqual(unit.container, ())
def test_archive_unit_container_clone(self):
"""Functional test for SEDA component clone."""
with self.admin_access.cnx() as cnx:
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment