Implement document replace operation and cover it with tests.