public function CRAUDFaq_AskTestCase::testFaq_AskCreate in FAQ_Ask 7
Test creating an unanswered FAQ and verify its status Also switches on notifcation of asker and verifies that anonymous are sent an e-mail by cron run
File
- ./
faq_ask.test, line 568 - Test Faq_Ask functionality Base test class. All tests inherits this one Hugely based on code from the test file block.test by boombatower
Class
Code
public function testFaq_AskCreate() {
$this
->drupalLogin($this->admin_user);
// Check anonymous creation of questions
$this
->turnBlockOff('faq_ask', 'ask_a_question');
// Enable asker and expert notification
$this
->setFaqAskSettings(array(
'faq_ask_asker_notify' => TRUE,
'faq_ask_notify' => TRUE,
));
// Change rights for anonymous user - Enable asking a question and viewing the faq page
$this
->setPermissions(NULL, array(
'ask question',
'create faq content',
));
// Done with admin user
$this
->drupalLogout();
// Set up faq-ask
// Anonymous user have access
$this
->drupalGet('faq_ask');
$this
->assertText(t('Ask a Question'), t('Ask a Question page is available for anonymous users.'));
$this
->assertNoFieldByName('body', $value = '', 'Body field (answer) is not visible in form.');
// Verify preloaded term
/*
$this->drupalGet('faq_ask/' . '1'); // Load url with term 1
$this->assertFieldByName('taxonomy[tags][1]', $this->term1['name'], 'Term preloaded on question form.');
*/
$taxonfield = 'taxonomy_' . $this->vocabulary->machine_name . '[und]';
// Fill in the Create FAQ node 1 form and post it - no email address
// Issue #1569684 by jlea9378: Not a valid e-mail address
$this->faq1 = array();
$this->faq1['title'] = $this
->randomName(8);
$this->faq1[$taxonfield] = $this->term->name;
// Add existing term
$this->faq1['detailed_question'] = $this
->randomName(16);
$this
->drupalPost('node/add/faq', $this->faq1, t('Save'));
$this->faq1['body'] = $this
->randomName(16);
// Check that new FAQ node has actually been created
$this
->assertText(t('FAQ @title has been created.', array(
'@title' => $this->faq1['title'],
)));
$this
->assertText(t('Your question has been submitted. It will appear in the FAQ listing as soon as it has been answered.'), 'Confirmation message detected');
// Fill in the Create FAQ node 1 form and post it
$this->faq3 = array();
$this->faq3['title'] = $this
->randomName(8);
$this->faq3[$taxonfield] = $this->term->name;
// Add existing term
$this->faq3['detailed_question'] = $this
->randomName(16);
$this->faq3['faq_email'] = $this
->randomName(8) . '@' . $this
->randomName(8) . '.com';
$this
->drupalPost('faq_ask', $this->faq3, t('Preview'));
// Preview first
// Verify preview
// $this->assertText('Not answered yet.', 'Not answered yet text shown.');
$this
->assertLink($this->faq3['title'], $index = 0, 'Link to question found');
// Verify field content is still available
$this
->assertFieldByName('title', $this->faq3['title'], 'Title field content kept after preview.');
$this
->assertFieldByName($taxonfield, $this->faq3[$taxonfield], 'Taxonomy term field content kept after preview.');
$this
->assertFieldByName('detailed_question', $this->faq3['detailed_question'], 'Detailed question field content kept after preview.');
$this
->assertFieldByName('faq_email', $this->faq3['faq_email'], 'E-mail field content kept after preview.');
// Post and save the question
$this
->drupalPost(NULL, array(), t('Save'));
// Now save
// Check that new FAQ node has actually been created
$this
->assertText(t('FAQ @title has been created.', array(
'@title' => $this->faq3['title'],
)));
$this
->assertText(t('Your question has been submitted. An e-mail will be sent to @mail when answered.', array(
'@mail' => $this->faq3['faq_email'],
)));
// Verify redirection to 'faq-page'
$this
->faqVerifyUrl('faq-page', 'Redirected to the faq-page');
$this
->assertText(t('Access denied'), 'No access to the faq-page');
$this
->drupalLogin($this->admin_user);
// Change rights for anonymous user - Enable asking a question and also viewing the faq page
$this
->setPermissions(NULL, array(
'ask question',
'create faq content',
'view faq page',
));
$this
->turnBlockOff('faq_ask', 'unanswered');
// Change rights for anonymous user - Enable viewing the FAQ-Page.
$this
->setPermissions(NULL, array(
'view faq page',
));
//*******************************************************************************
// Verify expert notification
// The email is sent in plain text. This means that <strong> and <i> are translated to * and /
$mails = $this
->drupalGetMails(array(
'to' => $this->answer_user2->mail,
));
$this
->pass('<pre>' . print_r($mails, TRUE) . '</pre>');
// Printout email
$mails = $this
->drupalGetMails(array(
'to' => $this->answer_user->mail,
));
$mail = $mails[1];
// Check the last one
$this
->assertEqual($mail['subject'], t('You have a question waiting on @site', array(
'@site' => variable_get('site_name', 'Drupal'),
)), 'Subject to anonymous correct');
// Find the body text of the e-mail
$lines = explode(chr(10) . chr(10), $mail['body']);
// Of some reason all mail lines are separated by two newlines
$this
->pass('<pre>' . print_r($this->faq3, TRUE) . '</pre>');
$this
->pass('<pre>' . print_r($lines, TRUE) . '</pre>');
$this
->assertEqual(trim($lines[0]), t('Dear @name,', array(
'@name' => $this->answer_user->name,
)), 'First e-mail line to expert correct');
$this
->assertEqual(trim(str_replace(chr(10), ' ', $lines[1])), $t = t('The following question has been posted in the "@term" category by Anonymous (not verified).', array(
'@term' => $this->term->name,
)), 'Second e-mail line reads:<br/>' . $lines[1] . '<br/>' . $t . ' expected.');
// $this->assertEqual(trim($lines[1]), t('The following question has been posted.'), 'First e-mail line to expert correct');
$this
->assertEqual(trim(str_replace(chr(10), ' ', $lines[2])), t('*/@title/*', array(
'@title' => $this->faq3['title'],
)), 'Third e-mail line: ' . $lines[2]);
$this
->assertEqual(trim(str_replace(chr(10), ' ', $lines[3])), t('/@detailed/', array(
'@detailed' => $this->faq3['detailed_question'],
)), 'Fourth e-mail line: ' . $lines[3]);
$this
->assertEqual(trim(str_replace(chr(10), ' ', $lines[4])), t('In order to answer it you will first need to login [1] to the site.', array(
'@url' => url('user', array(
'options' => array(
'absolute' => TRUE,
),
)),
)), 'Fifth e-mail line: ' . $lines[4]);
$node = $this
->drupalGetNodeByTitle($this->faq3['title']);
$url_options = array(
'options' => array(
'absolute' => TRUE,
),
'query' => array(
'token' => drupal_get_token('faq_ask/answer/' . $node->nid),
),
);
$options = array(
'@url' => url('faq_ask/answer/' . $node->nid, $url_options),
);
$this
->assertEqual(trim(str_replace(chr(10), ' ', $lines[5])), t('Once logged in, you may proceed directly to the question [2] to answer it.', $options), 'Sixth e-mail line: ' . $lines[5]);
$this
->assertEqual(trim(str_replace(chr(10), ' ', $lines[6])), t('By clicking on the above question link you will be redirected to the login form if you are currently logged out.'), 'Seventh e-mail line: ' . $lines[6]);
$this
->verboseEmail(2);
$this
->assertMail('to', $this->answer_user2->mail, t('E-mail was sent to expert 2: @mail', array(
'@mail' => $this->answer_user2->mail,
)));
$this
->assertMail('to', $this->answer_user->mail, t('E-mail was sent to expert: @mail', array(
'@mail' => $this->answer_user->mail,
)));
// Verify that the answer question link is correct and works
$url_array = explode('[2] ', $lines[7]);
$answer_url = trim($url_array[1]);
$url = parse_url($answer_url);
$this
->pass('<pre>url_array:' . var_export($url, TRUE) . '</pre>');
// Show us the node loaded
$token = explode('=', $url['query']);
$token_array = array(
$token[0] => $token[1],
);
// Should result in something like array('token' => 'vd1yS1Q2TQxBv9LQC7kTdzZxg-Ynl0lseAcHPkN18D8')
$answer_url_base = $url['path'];
// Get the 'faq_ask/answer/2' - part
$this
->pass('<pre>' . var_export($answer_url_base, TRUE) . '</pre>');
// Show us the node loaded
$this
->pass('<pre>' . var_export($token_array, TRUE) . '</pre>');
// Show us the node loaded
$answer_query = '';
//******************************************************************************
// Log in user with create FAQ permissions
$this
->drupalLogin($this->ask_user);
// Verify that the body field is not visible to the asker without answer permissions
$this
->drupalGet('node/add/faq');
$this
->assertNoText(t('Body'), t('Body field not visible to asker'));
// Create a FAQ Ask node.
$edit = array();
$edit['title'] = $this
->randomName(8);
$edit['taxonomy_' . $this->vocabulary->machine_name . '[und]'] = $this->term->name;
$edit['detailed_question'] = $this
->randomName(64);
$edit['faq_notify'] = TRUE;
// Create a notification message
$this
->drupalPost('node/add/faq', $edit, t('Save'));
$this
->assertText(t('FAQ @title has been created.', array(
'@title' => $edit['title'],
)));
// $this->assertText(t('Your question has been submitted. It will appear in the FAQ listing as soon as it has been answered.'));
$this
->assertText(t('Your question has been submitted. An e-mail will be sent to @mail when answered.', array(
'@mail' => $this->ask_user->mail,
)));
// Check status for FAQ node - should be not published
$node = $this
->drupalGetNodeByTitle($edit['title']);
//$this->pass('<pre>' . var_export($node, TRUE) . '</pre>'); // Show us the node loaded
$this
->assertFalse($node->status);
// FAQ node should not be visible for user with view faq permissions
$this
->drupalLogin($this->ask_user);
$this
->drupalGet("faq-page");
// Load faq page
$this
->assertNoText($edit['title']);
// Node should not be listed here
$this
->drupalGet('node/' . $node->nid);
// But should be possible to open the node directly by the author who created it
$this
->assertResponse(200);
$this
->drupalLogin($this->admin_user);
$this
->turnBlockOn('faq_ask', 'unanswered');
// Question is reassigned only when anonymous
$this
->setFaqAskSettings(array(
'faq_ask_expert_own' => '1',
));
// Reassigned only when anonymous
// Verify answer link from e-mail to answer faq3 question
$this
->drupalLogout();
$this
->drupalGet($answer_url);
$this
->assertText('User account', t('Answer link with token redirects to login page when clicked and not logged in.'));
$this
->assertText('Log in', t('Answer link with token redirects to login page when clicked and not logged in.'));
$this
->drupalLogin($this->answer_user);
$this
->drupalGet($answer_url_base, array(
'query' => $token_array,
));
$this
->assertText($t = t('Edit FAQ @title', array(
'@title' => $this->faq3['title'],
)), t('On answering page of question when using link in e-mail: @text', array(
'@text' => $t,
)));
$this
->drupalPost(NULL, array(
'body[und][0][value]' => $this
->randomName(64),
), t('Save'));
// Log in user with answer question. Must also have edit faq and view faq page permission
$this
->drupalLogin($this->answer_user);
// Open unanswered question
// Verify that logged in user has no access to the unanswered node display
$this
->faqVerifyNoAccess('node/' . $node->nid);
$this
->drupalGet('node/' . $node->nid . '/edit');
// Open edit page with node
$this
->assertResponse(200);
// Select question posted by registered user
$this
->clickLink($edit['title']);
// Click the node name in unanswered questions box
// Verify status message when question is reassigned
$this
->assertNoText(t('This question is being assigned to @name.', array(
'@name' => $this->answer_user->name,
)), t('Question is not assigned to expert'));
// Post an answer to the question
$this
->drupalPost(NULL, array(
'body[und][0][value]' => $this
->randomName(64),
), t('Save'));
// Click the anonymous user question
$this
->clickLink($this->faq1['title']);
// Click the node name in unanswered questions box
// Verify status message when question is reassigned
$this
->assertText(t('This question is being assigned to @name.', array(
'@name' => $this->answer_user->name,
)), t('Question is assigned to expert'));
// Create and log in user with view FAQ permissions
$faq_view_user = $this
->drupalCreateUser(array(
'view faq page',
));
$this
->drupalLogin($faq_view_user);
// Verify visibility on faq page
$this
->drupalGet("faq-page");
// Load faq page
$this
->assertText($edit['title']);
// Node should be listed here
$this
->drupalGet('node/' . $node->nid);
// It should also be possible to open the node directly
// Update FAQ
// Log in user with answer question. Must also have edit faq and view faq page permission
$this
->drupalLogin($this->answer_user);
$edit2['title'] = 'title-' . $this
->randomName(8);
$edit2['body[und][0][value]'] = 'body-' . $this
->randomName(64);
$this
->drupalPost('node/' . $node->nid . '/edit', array(
'title' => $edit2['title'],
'body[und][0][value]' => $edit2['body[und][0][value]'],
), t('Save'));
$this
->assertText(t('FAQ @title has been updated.', array(
'@title' => $edit2['title'],
)));
$this
->assertText($edit2['title'], 'Title has changed');
$this
->assertText($edit2['body[und][0][value]'], 'Body has changed');
// Delete FAQ
// Try deleting faq by edit faq permission
$this
->drupalGet('node/' . $node->nid . '/edit');
$this
->assertNoText('Delete', 'Body has changed');
// Log in user with delete FAQ permissions
$this
->drupalLogin($this->admin_user);
$this
->drupalPost('node/' . $node->nid . '/edit', array(), t('Delete'));
$this
->assertText(t('Are you sure you want to delete @title?', array(
'@title' => $edit2['title'],
)));
$this
->drupalPost('node/' . $node->nid . '/delete', array(), t('Delete'));
$this
->assertText(t('FAQ @title has been deleted.', array(
'@title' => $edit2['title'],
)));
$this
->cronRun();
// Verify answer messages being sent
$faq3_node = $this
->drupalGetNodeByTitle($this->faq3['title']);
// Get the e-mail to the anonymous asker
$mail = array_shift($this
->drupalGetMails(array(
'to' => $this->faq3['faq_email'],
)));
$this
->assertEqual($mail['subject'], t('A question you asked has been answered on @site', array(
'@site' => variable_get('site_name', 'Drupal'),
)), 'Subject to anonymous correct');
$lines = explode(chr(10) . chr(10), $mail['body']);
$this
->assertEqual(trim($lines[0]), t('Dear user,'), 'First e-mail line to anonymous correct');
$this
->assertEqual(trim($lines[1]), t('The question: "@title" you asked on @site has been answered.', array(
'@site' => variable_get('site_name', 'Drupal'),
'@title' => $this->faq3['title'],
)), 'Second e-mail line to anonymous correct');
$lines[2] = str_replace(chr(10), ' ', $lines[2]);
// Creates something like: "To view the answer, please visit the question you created on http://d7.example.com/?q=node/2."
// $this->assertEqual(trim($lines[2]), t('To view the answer, please visit the question you created on @url.', array('@url' => url('node/' . $faq3_node->nid, array('options' => array('absolute' => TRUE))))), 'Third e-mail line :' . $lines[2] );
$this
->assertEqual(trim($lines[3]), t('Thank you for visiting.'), 'Fourth e-mail line:' . $lines[3]);
$this
->verboseEmail(2);
$this
->assertMail('to', $this->faq3['faq_email'], t('E-mail was sent to anonymous asker: @mail', array(
'@mail' => $this->faq3['faq_email'],
)));
// $this->assertMail('subject', t('A question you asked has been answered on'), t('E-mail was sent to the registered asker'));
/*
$byteline = '';
for ($i = 0; $i < strlen($mail['body']); $i++)
{
$byteline .= ' ' . ord($mail['body'][$i]);
}
//foreach ((array)($mail['body']) as $i => $c) $byteline[$i] = ord($c);
//$bytestring = implode(' ', $byteline);
*/
$this
->pass('<pre>' . print_r($lines, TRUE) . '</pre>');
}