Fix #1314
Unit test
(cherry picked from commit b64b352f601068da16710a956e6519fa74974a69)
Fix unrelated bug in the test