// clean all the comments related to the test resource
$comments = sfEmendCommentPeer::getAllCommentsForResource($resource);
foreach ($comments as $comment) {
    $comment->delete($con);
}
$user_options = sfConfig::get('app_sfEmendPlugin_user');
$browser = new sfTestBrowser();
$browser->initialize();
// get empty comments list for the first resource
$browser->test()->comment('Get an empty comments list');
$browser->get("/emend.getComments/" . $resource)->isStatusCode(200)->isRequestParameter('module', 'sfEmendAPI')->isRequestParameter('action', 'getComments')->responseContains('"n_comments": 0');
// try to add a comment through an emulated POST, as an anonymous user with no author name
$browser->test()->comment('Try to add a comment as anonymous user');
$browser->post("/emend.addComment/" . $resource, array('title' => 'Prova', 'body' => 'Un commento di prova', 'selection' => '"s": []'))->isStatusCode(200);
if ($user_options['allow_anonymous'] == 1) {
    $browser->responseContains('Anonymous posting requires an author_name parameter');
} else {
    $browser->responseContains('Anonymous posting not allowed');
}
// add a comment through an emulated POST,
// as an anonymous user with author name
$browser->test()->comment('Try to add a comment as anonymous user with author name');
$browser->post("/emend.addComment/" . $resource, array('title' => 'Prova', 'body' => 'Un commento di prova', 'selection' => '"s": []', 'author_name' => 'Guglielmo Celata'))->isStatusCode(200);
if ($user_options['allow_anonymous'] == 1) {
    $browser->responseContains('"s":');
} else {
    $browser->responseContains('Anonymous posting not allowed');
}
$browser->test()->comment('Login through the browser');
$browser->get("/login")->isStatusCode(401)->isRequestParameter('module', 'sfGuardAuth')->isRequestParameter('action', 'signin')->post('/login', array('username' => '*****@*****.**', 'password' => 'Vakka94', 'commit' => 'sign in'))->isRedirected()->followRedirect()->isStatusCode(200)->isRequestParameter('module', 'default')->isRequestParameter('action', 'index')->checkResponseElement('div[id="login"] div[class="inner"] span', "/Guglielmo/");
// try to add a comment through an emulated POST, as an authenticated user