Refactor importmetadata
This fixes #2719 (closed), #2720 (closed), #2724 (closed) and makes importMetadata unit-testable
Not sure it makes a lot of sense to include it since we plan to replace importMetadata entirely (#2731 (closed))
What do you think @clement_oudot ?