Fixing $query->setLimit (with bonus unit tests)

See https://github.com/joomla/joomla-cms/pull/3945