mw.util.addPortletLink: Check length before access array index

This avoids "TypeError: Cannot read property 'parentNode' of undefined",
when the given nextNode is a jQuery object without elements.

Adding a test for this case and for the case of unknown id, which
internally results in a empty jQuery object.

Bug: 56770
Change-Id: I56de9e6bc15449e294b538743baf0f0c1cc0abb1
(cherry picked from commit c5eae531868df4f37c6f4ce6ac2506adf7528bd0)