Backport changes from cakephp/cakephp#3959 to 1.3

Backport race condition fixes from 2.x to 1.3. This is a severe enough
issue that it warrants a fix.

Refs #3857
Fix failing tests.

These tests were failing as integers were not being quoted as strings
which is expected for me at least.