diff --git a/src/LimeSurvey/Service/SurveyService.php b/src/LimeSurvey/Service/SurveyService.php index 58c0db0..066e496 100644 --- a/src/LimeSurvey/Service/SurveyService.php +++ b/src/LimeSurvey/Service/SurveyService.php @@ -191,6 +191,23 @@ class SurveyService return false; } + /** + * Returns url used to start survey for given survey and participant's token + * + * @param int $surveyId ID of survey to start + * @param string $participantToken Token of participant who would like to start survey + * @return string + */ + public function getStartSurveyUrlByToken($surveyId, $participantToken) + { + $baseUrl = $this + ->client + ->getConfiguration() + ->getBaseUrl(); + + return sprintf($this->startSurveyUrlTemplate, $baseUrl, $surveyId, $participantToken); + } + /** * Returns url used to start survey for given survey and participant * @@ -200,12 +217,7 @@ class SurveyService */ public function getStartSurveyUrl($surveyId, Participant $participant) { - $baseUrl = $this - ->client - ->getConfiguration() - ->getBaseUrl(); - - return sprintf($this->startSurveyUrlTemplate, $baseUrl, $surveyId, $participant->getToken()); + return $this->getStartSurveyUrlByToken($surveyId, $participant->getToken()); } /** diff --git a/tests/Service/SurveyServiceTest.php b/tests/Service/SurveyServiceTest.php index 7b87b66..de69d23 100644 --- a/tests/Service/SurveyServiceTest.php +++ b/tests/Service/SurveyServiceTest.php @@ -181,6 +181,22 @@ class SurveyServiceTest extends BaseTestCase static::assertFalse($this->serviceWithSurveys->isExistingSurvey(4, true)); } + public function testGetStartSurveyUrlByToken() + { + $rpcClientManager = $this->getJsonRpcClientManager(0); + $sessionManager = $this->getSessionManager(); + + $this->createServiceWithoutSurveys($rpcClientManager, $sessionManager); + $this->createServiceWithSurveys($rpcClientManager, $sessionManager); + + $surveyId = 123; + $token = uniqid(); + $expectedUrl = sprintf('%s/%d?token=%s', $this->connectionBaseUrl, $surveyId, $token); + + static::assertEquals($expectedUrl, $this->serviceWithoutSurveys->getStartSurveyUrlByToken($surveyId, $token)); + static::assertEquals($expectedUrl, $this->serviceWithSurveys->getStartSurveyUrlByToken($surveyId, $token)); + } + public function testGetStartSurveyUrl() { $rpcClientManager = $this->getJsonRpcClientManager(0);