public function test_entity_tag_updating()
{
$page = $this->getPageWithTags();
$testJsonData = [['name' => 'color', 'value' => 'red'], ['name' => 'color', 'value' => ' blue '], ['name' => 'city', 'value' => 'London '], ['name' => 'country', 'value' => ' England']];
$testResponseJsonData = [['name' => 'color', 'value' => 'red'], ['name' => 'color', 'value' => 'blue'], ['name' => 'city', 'value' => 'London'], ['name' => 'country', 'value' => 'England']];
// Do update request
$this->asAdmin()->json("POST", "/ajax/tags/update/page/" . $page->id, ['tags' => $testJsonData]);
$updateData = json_decode($this->response->getContent());
// Check data is correct
$testDataCorrect = true;
foreach ($updateData->tags as $data) {
$testItem = ['name' => $data->name, 'value' => $data->value];
if (!in_array($testItem, $testResponseJsonData)) {
$testDataCorrect = false;
}
}
$testMessage = "Expected data was not found in the response.\nExpected Data: %s\nRecieved Data: %s";
$this->assertTrue($testDataCorrect, sprintf($testMessage, json_encode($testResponseJsonData), json_encode($updateData)));
$this->assertTrue(isset($updateData->message), "No message returned in tag update response");
// Do get request
$this->asAdmin()->get("/ajax/tags/get/page/" . $page->id);
$getResponseData = json_decode($this->response->getContent());
// Check counts
$this->assertTrue(count($getResponseData) === count($testJsonData), "The received tag count is incorrect");
// Check data is correct
$testDataCorrect = true;
foreach ($getResponseData as $data) {
$testItem = ['name' => $data->name, 'value' => $data->value];
if (!in_array($testItem, $testResponseJsonData)) {
$testDataCorrect = false;
}
}
$testMessage = "Expected data was not found in the response.\nExpected Data: %s\nRecieved Data: %s";
$this->assertTrue($testDataCorrect, sprintf($testMessage, json_encode($testResponseJsonData), json_encode($getResponseData)));
}