metatag.helper.test 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. <?php
  2. /**
  3. * A base class for the Metatag tests, provides shared methods.
  4. */
  5. abstract class MetatagTestHelper extends DrupalWebTestCase {
  6. /**
  7. * Admin user.
  8. *
  9. * @var \StdClass
  10. */
  11. protected $adminUser;
  12. /**
  13. * {@inheritdoc}
  14. */
  15. function setUp(array $modules = array()) {
  16. // Make sure these modules are enabled so that we can use their entity
  17. // types later.
  18. $modules[] = 'node';
  19. $modules[] = 'taxonomy';
  20. // Requirements.
  21. $modules[] = 'ctools';
  22. $modules[] = 'token';
  23. // Metatag modules. Only enable the main module, submodules will be tested
  24. // separately.
  25. $modules[] = 'metatag';
  26. // Adds some functionality for testing the entity handling.
  27. $modules[] = 'metatag_test';
  28. parent::setUp($modules);
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. protected function verbose($message, $title = NULL) {
  34. // Handle arrays, objects, etc.
  35. if (!is_string($message)) {
  36. $message = "<pre>\n" . print_r($message, TRUE) . "\n</pre>\n";
  37. }
  38. // Optional title to go before the output.
  39. if (!empty($title)) {
  40. $title = '<h2>' . check_plain($title) . "</h2>\n";
  41. }
  42. parent::verbose($title . $message);
  43. }
  44. /**
  45. * Load the Performance admin page and clear all caches.
  46. */
  47. function clearAllCaches() {
  48. $this->drupalGet('admin/config/development/performance');
  49. $this->assertResponse(200);
  50. $this->assertText(t('Performance'));
  51. $this->assertText(t('Clear cache'));
  52. $this->drupalPost(NULL, array(), t('Clear all caches'));
  53. $this->assertResponse(200);
  54. $this->assertText(t('Caches cleared'));
  55. }
  56. /**
  57. * Create a content type for the tests.
  58. */
  59. function createContentType($machine_name, $label) {
  60. // Create a content type.
  61. $content_type = $this->drupalCreateContentType(array(
  62. 'type' => $machine_name,
  63. 'name' => $label,
  64. ));
  65. // Enable meta tags for this new content type.
  66. metatag_entity_type_enable('node', $machine_name, TRUE);
  67. return $content_type;
  68. }
  69. /**
  70. * Create an admin user for the tests.
  71. *
  72. * @param array $extra_permissions
  73. * An array of permission strings to be added to the user.
  74. *
  75. * @return object
  76. * A user object.
  77. */
  78. function createAdminUser($extra_permissions = array()) {
  79. $permissions = array(
  80. // Basic permissions for the module.
  81. 'administer meta tags',
  82. 'edit meta tags',
  83. // General admin access.
  84. 'access administration pages',
  85. );
  86. // Reset the static variable used to identify permissions, otherwise it's
  87. // possible the permissions check in drupalCreateUser will fail.
  88. $this->checkPermissions(array(), TRUE);
  89. cache_clear_all();
  90. return $this->drupalCreateUser(array_merge($permissions, $extra_permissions));
  91. }
  92. /**
  93. * Create a normal user for the tests.
  94. *
  95. * @param array $extra_permissions
  96. * An array of permission strings to be added to the user.
  97. *
  98. * @return object
  99. * A user object.
  100. */
  101. function createUser($extra_permissions) {
  102. // Basic permissions for the module.
  103. $permissions = array(
  104. 'edit meta tags',
  105. );
  106. // Reset the static variable used to identify permissions, otherwise it's
  107. // possible the permissions check in drupalCreateUser will fail.
  108. $this->checkPermissions(array(), TRUE);
  109. cache_clear_all();
  110. return $this->drupalCreateUser(array_merge($permissions, $extra_permissions));
  111. }
  112. /**
  113. * Returns a new vocabulary with random properties.
  114. *
  115. * @param $vocab_name
  116. * If empty a random string will be used.
  117. * @param $content_type
  118. * Any content types listed will have a Taxonomy Term reference field added
  119. * that points to the new vocabulary.
  120. *
  121. * @return object
  122. * A vocabulary object.
  123. */
  124. function createVocabulary($vocab_name = NULL, $content_type = NULL) {
  125. if (empty($vocab_name)) {
  126. $vocab_name = $this->randomName();
  127. }
  128. // Create a vocabulary.
  129. $vocabulary = new stdClass();
  130. $vocabulary->name = $vocab_name;
  131. $vocabulary->description = $vocab_name;
  132. $vocabulary->machine_name = drupal_strtolower($vocab_name);
  133. $vocabulary->help = '';
  134. $vocabulary->weight = mt_rand(0, 10);
  135. if (!empty($content_type)) {
  136. $vocabulary->nodes = array($content_type => $content_type);
  137. }
  138. taxonomy_vocabulary_save($vocabulary);
  139. // Enable meta tags for this new vocabulary.
  140. metatag_entity_type_enable('taxonomy_term', $vocab_name, TRUE);
  141. return $vocabulary;
  142. }
  143. /**
  144. * Returns a new taxonomy term in a specific vocabulary.
  145. *
  146. * @param object $vocabulary
  147. * The vocabulary to add the term to.
  148. * @param string $term_name
  149. * The name to use for the new vocabulary. If none is provided one will be
  150. * generated randomly.
  151. *
  152. * @return object
  153. * A taxonomy term object.
  154. */
  155. function createTerm($vocabulary, $term_name = NULL) {
  156. if (empty($term_name)) {
  157. $term_name = $this->randomName();
  158. }
  159. // Create an object to save.
  160. $term = new stdClass();
  161. $term->name = $term_name;
  162. $term->description = $term_name;
  163. // Use the first available text format.
  164. $term->format = db_query_range('SELECT format FROM {filter_format}', 0, 1)->fetchField();
  165. $term->vid = $vocabulary->vid;
  166. // Save the term.
  167. taxonomy_term_save($term);
  168. return $term;
  169. }
  170. /**
  171. * Return an list of default values.
  172. *
  173. * This should cover all of the default meta tags provided for a test:foo
  174. * entity.
  175. *
  176. * @todo Expand to cover more meta tags.
  177. *
  178. * @see metatag_test_metatag_config_default()
  179. */
  180. function getTestDefaults() {
  181. return array(
  182. // Basic meta tags.
  183. 'title' => array('value' => 'Test altered title'),
  184. 'description' => array('value' => 'Test foo description'),
  185. 'abstract' => array('value' => 'Test foo abstract'),
  186. // 'keywords' => array('value' => ''),
  187. // Advanced meta tags.
  188. // 'robots' => array('value' => ''),
  189. // 'news_keywords' => array('value' => ''),
  190. // 'standout' => array('value' => ''),
  191. // 'robots' => array('value' => ''),
  192. // 'standout' => array('value' => ''),
  193. 'generator' => array('value' => 'Drupal 7 (http://drupal.org)'),
  194. // 'standout' => array('value' => ''),
  195. // 'image_src' => array('value' => ''),
  196. 'canonical' => array('value' => '[current-page:url:absolute]'),
  197. 'shortlink' => array('value' => '[current-page:url:unaliased]'),
  198. // 'publisher' => array('value' => ''),
  199. // 'author' => array('value' => ''),
  200. // 'original-source' => array('value' => ''),
  201. // 'revisit-after' => array('value' => ''),
  202. // 'content-language' => array('value' => ''),'
  203. );
  204. }
  205. /**
  206. * Add a locale to the site.
  207. *
  208. * This assumes the Locale module is enabled.
  209. */
  210. public function addSiteLanguage($langcode) {
  211. // Load the language-add page.
  212. $this->drupalGet('admin/config/regional/language/add');
  213. $this->assertResponse(200, 'Loaded the language-add admin page.');
  214. // Submit the language-add form.
  215. $args = array(
  216. 'langcode' => $langcode,
  217. );
  218. $this->drupalPost(NULL, $args, t('Add language'));
  219. $this->assertResponse(200);
  220. // Verify that the browser was returned to the main languages admin page.
  221. $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), 'Redirected back to the main languages admin page.');
  222. // Clear the language list cache so it can be reloaded.
  223. drupal_static_reset('language_list');
  224. // Get all language definitions.
  225. $languages = language_list();
  226. $language = $languages[$langcode]->name;
  227. $this->assertText(strip_tags(t('The language %language has been created and can now be used. More information is available on the <a href="@locale-help">help screen</a>.', array('%language' => t($language), '@locale-help' => url('admin/help/locale')))), 'A new language has been added.');
  228. }
  229. /**
  230. * Set up a basic starting point for the locales.
  231. *
  232. * This assumes the Locale module is enabled. This also must be done before
  233. * other user accounts are logged in.
  234. *
  235. * @param array $locales
  236. * A list of locales to be enabled, in langcode format.
  237. */
  238. public function setupLocales(array $locales = array()) {
  239. // If no locales were requested, add Spanish and French.
  240. if (empty($locales)) {
  241. $locales[] = 'es';
  242. $locales[] = 'fr';
  243. }
  244. // Log in as an admin user with privs to just set up the locales.
  245. $perms = array(
  246. 'administer languages',
  247. 'translate interface',
  248. 'access administration pages',
  249. );
  250. $admin_user = $this->drupalCreateUser($perms);
  251. $this->drupalLogin($admin_user);
  252. // Load the admin page, just to have a point of reference.
  253. $this->drupalGet('admin');
  254. $this->assertResponse(200, 'Loaded the main admin page.');
  255. // Identify the site's default language.
  256. $default_language = language_default('language');
  257. // Add the locales.
  258. foreach ($locales as $langcode) {
  259. // Don't create the default language, it's already present.
  260. if ($langcode != $default_language) {
  261. $this->addSiteLanguage($langcode);
  262. }
  263. }
  264. // Enable URL language detection and selection.
  265. $this->drupalGet('admin/config/regional/language/configure');
  266. $this->assertResponse(200);
  267. $edit = array(
  268. 'language[enabled][locale-url]' => TRUE,
  269. );
  270. // Also enable path handling for Entity Translation if it is installed.
  271. if (module_exists('entity_translation')) {
  272. $edit['language_content[enabled][locale-url]'] = TRUE;
  273. }
  274. $this->drupalPost(NULL, $edit, t('Save settings'));
  275. $this->assertResponse(200);
  276. // Once all the setup is done, log out the user.
  277. $this->drupalLogout();
  278. }
  279. /**
  280. * Get the {locales_source} lid value for a specific context.
  281. *
  282. * @param string $context
  283. * The context string to search for.
  284. * @param string $textgroup
  285. * This string's textgroup; defaults to 'metatag'.
  286. *
  287. * @return integer
  288. * The {locales_source}.lid value for this string.
  289. */
  290. function getTranslationLidByContext($context, $textgroup = 'metatag') {
  291. // Extra debug output.
  292. $this->debugLocalesSourcesByContext($context);
  293. // Look for the string that's actually being requested.
  294. return (int) db_query("SELECT lid
  295. FROM {locales_source}
  296. WHERE textgroup = :textgroup
  297. AND context = :context",
  298. array(
  299. ':textgroup' => $textgroup,
  300. ':context' => $context,
  301. ))
  302. ->fetchField();
  303. }
  304. /**
  305. * Get the {locales_source} lid value for a specific source.
  306. *
  307. * @param string $string
  308. * The translation string to search for.
  309. * @param string $textgroup
  310. * This string's textgroup; defaults to 'metatag'.
  311. *
  312. * @return integer
  313. * The {locales_source}.lid value for this string.
  314. */
  315. function getTranslationLidBySource($string, $textgroup = 'metatag') {
  316. // Extra debug output.
  317. $this->debugLocalesSourcesByContext('', $textgroup);
  318. // Look for the string that's actually being requested.
  319. return (int) db_query("SELECT lid
  320. FROM {locales_source}
  321. WHERE textgroup = :textgroup
  322. AND source = :source",
  323. array(
  324. ':textgroup' => $textgroup,
  325. ':source' => $string,
  326. ))
  327. ->fetchField();
  328. }
  329. /**
  330. * Get the {locales_source} lid values for a specific context.
  331. *
  332. * @param string $context
  333. * The context string to search for.
  334. * @param string $textgroup
  335. * This string's textgroup; defaults to 'metatag'.
  336. *
  337. * @return integer
  338. * The {locales_source}.lid value for this string.
  339. */
  340. function getTranslationsByContext($context, $textgroup = 'metatag') {
  341. return db_query("SELECT lid
  342. FROM {locales_source}
  343. WHERE textgroup = :textgroup
  344. AND context = :context",
  345. array(
  346. ':textgroup' => $textgroup,
  347. ':context' => $context,
  348. ))
  349. ->fetchCol();
  350. }
  351. /**
  352. * Generate a debug dump of the {locales_source} records for a specific context.
  353. *
  354. * @param string $context
  355. * The translation context to search against.
  356. * @param string $textgroup
  357. * This string's textgroup; defaults to 'metatag'.
  358. */
  359. function debugLocalesSourcesByContext($context, $textgroup = 'metatag') {
  360. // Get a dump of all i18n strings for Metatag.
  361. $records = db_query("SELECT lid, location, textgroup, source, context, version
  362. FROM {locales_source}
  363. WHERE textgroup = :textgroup",
  364. array(
  365. ':textgroup' => $textgroup,
  366. ))
  367. ->fetchAllAssoc('lid');
  368. foreach ($records as $key => $record) {
  369. $records[$key] = (array) $record;
  370. }
  371. $args = array(
  372. 'caption' => 'i18n source check for . ' . $context,
  373. 'header' => array(
  374. 'lid',
  375. 'location',
  376. 'textgroup',
  377. 'source',
  378. 'context',
  379. 'version',
  380. ),
  381. 'rows' => $records,
  382. );
  383. $this->verbose(theme('table', $args));
  384. }
  385. /**
  386. * Save a {locales_target} translation string to the database.
  387. *
  388. * @param int $lid
  389. * The {locales_source}.lid primary key.
  390. * @param string $context
  391. * The {locales_source}.context value for this string.
  392. * @param string $langcode
  393. * The language the string is being translated into.
  394. * @param string $string_source
  395. * The string that is being translated.
  396. * @param string $string_target
  397. * The destination string.
  398. */
  399. function saveTranslationString($lid, $context, $langcode, $string_source, $string_target) {
  400. // Load the translation page for the front page's title tag.
  401. $this->drupalGet('admin/config/regional/translate/edit/' . $lid);
  402. $this->assertResponse(200, 'Loaded the front page title tag string translation page.');
  403. $this->assertEqual($this->getUrl(), url('admin/config/regional/translate/edit/' . $lid, array('absolute' => TRUE)));
  404. // Confirm that the permission-check text is not found.
  405. $this->assertNoText(t('This is a user-defined string. You are not allowed to translate these strings.'));
  406. // Look for the existing string. The string gets mungled by the Locale
  407. // module, so need to replicate its behaviour.
  408. $this->assertText(check_plain(wordwrap($string_source, 0)));
  409. // Look for the context value; the context value is empty for all default
  410. // i.e. interface strings, so don't test this when the context is empty.
  411. if (!empty($context)) {
  412. $this->assertText($context);
  413. }
  414. // Confirm that the destination strings exist.
  415. $source_locale = language_default('language');
  416. if (function_exists('i18n_string_source_language')) {
  417. $source_locale = i18n_string_source_language();
  418. }
  419. if ($source_locale != 'en') {
  420. $this->assertField('translations[en]', 'Found the English translation string field.');
  421. }
  422. if ($source_locale != 'fr') {
  423. $this->assertField('translations[fr]', 'Found the French translation string field.');
  424. }
  425. if ($source_locale != 'es') {
  426. $this->assertField('translations[es]', 'Found the Spanish translation string field.');
  427. }
  428. // Translate the string.
  429. $edit = array(
  430. "translations[{$langcode}]" => $string_target,
  431. );
  432. $this->drupalPost(NULL, $edit, t('Save translations'));
  433. $this->assertResponse(200);
  434. // Confirm the save worked.
  435. $this->assertText(t('The string has been saved.'));
  436. $this->assertEqual($this->getUrl(), url('admin/config/regional/translate/translate', array('absolute' => TRUE)));
  437. // Debug output.
  438. $this->debugLocalesTargetsByContext($context);
  439. // Clear the Metatag caches.
  440. metatag_flush_caches();
  441. }
  442. /**
  443. * Generate a debug dump of the {locales_target} records for a specific context.
  444. *
  445. * @param string $context
  446. * The translation context to search against.
  447. */
  448. function debugLocalesTargetsByContext($context) {
  449. // , lt.i18n_status
  450. $records = db_query("SELECT lt.lid, lt.translation, lt.language, lt.plid, lt.plural
  451. FROM {locales_target} lt
  452. INNER JOIN {locales_source} ls
  453. ON lt.lid = ls.lid
  454. WHERE ls.textgroup = 'metatag'
  455. AND ls.context = :context",
  456. array(':context' => $context))
  457. ->fetchAllAssoc('lid');
  458. foreach ($records as $key => $record) {
  459. $records[$key] = (array) $record;
  460. }
  461. $args = array(
  462. 'caption' => 'Locale target check for . ' . $context,
  463. 'header' => array(
  464. 'lid',
  465. 'translation',
  466. 'language',
  467. 'plid',
  468. 'plural',
  469. // 'i18n_status',
  470. ),
  471. 'rows' => $records,
  472. );
  473. $this->verbose(theme('table', $args));
  474. }
  475. /**
  476. * Creates an object which can be used for generating and checking behavior.
  477. *
  478. * @param string $identifier
  479. * The machine name to identify this object in source code.
  480. * @param string $path
  481. * Path where generate metatags.
  482. *
  483. * @return object
  484. * A mapping object.
  485. */
  486. function createTestObject($identifier, $path) {
  487. $test_object = new stdClass();
  488. $test_object->name = $identifier;
  489. $test_object->path = $path;
  490. $test_object->title = "My $identifier title";
  491. $test_object->description = "My $identifier description";
  492. $test_object->abstract = "My $identifier abstract";
  493. $test_object->keywords = "My $identifier keywords";
  494. return $test_object;
  495. }
  496. /**
  497. * Generates meta tags by path from a test_object.
  498. *
  499. * @return $test_object
  500. * Metatag mapping object.
  501. */
  502. function generateByPathConfig($test_object) {
  503. // Verify the "add context" page works.
  504. $this->drupalGet('admin/config/search/metatags/context');
  505. $this->assertResponse(200);
  506. $this->assertText(t('Add a meta tag by path'));
  507. // Verify the "add context" page works.
  508. $this->drupalGet('admin/config/search/metatags/context/add');
  509. $this->assertResponse(200);
  510. $this->assertText(t('The unique ID for this metatag path context rule. This must contain only lower case letters, numbers and underscores.'));
  511. // Add new Metatag object for this configuration.
  512. $values = array(
  513. 'name' => $test_object->name,
  514. );
  515. $this->drupalPost('admin/config/search/metatags/context/add', $values, t('Add and configure'));
  516. $this->assertResponse(200);
  517. }
  518. /**
  519. * Edits meta tags by path from a test_object.
  520. *
  521. * @return $test_object
  522. * Metatag mapping object.
  523. */
  524. function editByPathConfig($test_object) {
  525. $edit = array(
  526. 'paths' => $test_object->path,
  527. 'metatags[und][title][value]' => $test_object->title,
  528. 'metatags[und][description][value]' => $test_object->description,
  529. 'metatags[und][abstract][value]' => $test_object->abstract,
  530. 'metatags[und][keywords][value]' => $test_object->keywords,
  531. );
  532. $this->drupalPost('admin/config/search/metatags/context/' . $test_object->name, $edit, t('Save'));
  533. $this->assertResponse(200);
  534. }
  535. /**
  536. * Checks if meta tags have been added correctly from a test_object.
  537. *
  538. * @return $test_object
  539. * Metatag mapping object.
  540. */
  541. function checkByPathConfig($test_object) {
  542. $this->drupalGet($test_object->path);
  543. $this->assertResponse(200);
  544. // Manually test the page title.
  545. if (!empty($test_object->title)) {
  546. $this->assertTitle($test_object->title, 'Title found in ' . $test_object->path);
  547. }
  548. // Test the other meta tags.
  549. $tags = array('description', 'abstract', 'keywords');
  550. foreach ($tags as $tag) {
  551. if (!empty($test_object->{$tag})) {
  552. $this->assertRaw($test_object->{$tag}, $tag . ' found in ' . $test_object->path);
  553. }
  554. else {
  555. $this->assertNoRaw('<meta name="' . $tag, $tag . ' not found in ' . $test_object->path);
  556. }
  557. }
  558. }
  559. /**
  560. * Check the translation page for a specific string.
  561. *
  562. * @param string $string
  563. * The source string to search for.
  564. * @param string $context
  565. * The i18n string context to check for.
  566. * @param boolean $assert_true
  567. * By default strings are expeted to be found. If the string is not expected
  568. * to be translatable yet then pass in FALSE.
  569. */
  570. function searchTranslationPage($string, $context, $assert_true = TRUE) {
  571. // Load the translation page.
  572. $search = array(
  573. 'string' => $string,
  574. 'language' => 'all',
  575. 'translation' => 'all',
  576. 'group' => 'metatag',
  577. );
  578. $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  579. $this->assertResponse(200, 'Loaded the translation admin page.');
  580. // Confirm there are strings to translate.
  581. $xpath = $this->xpath("//body//div[@class='content']//table//tbody//tr");
  582. $count = count($xpath);
  583. // If the string is expected, then confirm that there are strings to be
  584. // found.
  585. if ($assert_true) {
  586. $this->assertTrue($count >= 1, 'Found Metatag strings to translate.');
  587. $this->assertNoText(t('No strings available.'));
  588. $xpath = $this->xpath("//body//div[@class='content']//table//tbody//tr");
  589. $this->verbose("Found {$count} items to translate.");
  590. if (!empty($string)) {
  591. $this->assertText($context);
  592. $this->assertText('metatag:' . $context);
  593. }
  594. }
  595. // If the string is not supposed to be found, then confirm the context is
  596. // not available.
  597. else {
  598. $this->assertNoText($context);
  599. $this->assertNoText('metatag:' . $context);
  600. }
  601. }
  602. /**
  603. * Create an image of a specific size & type.
  604. *
  605. * @param string $image_size
  606. * The size of the requested image in 'XxY' format; defaults to '200x200'.
  607. * @param string $format
  608. * The image format to use, defaults to 'png'.
  609. *
  610. * @return string
  611. * The URL to a public file.
  612. */
  613. function generateImage($image_size = '200x200', $format = 'png') {
  614. // Only proceed if the Devel Generate module is installed.
  615. if (module_exists('devel_generate')) {
  616. // Load the Devel Generate image generator logic.
  617. module_load_include('inc', 'devel_generate', 'image.devel_generate');
  618. $image_format = 'png';
  619. $image_size = '200x200';
  620. $temp_image = devel_generate_image($image_format, $image_size, $image_size);
  621. return file_unmanaged_move($temp_image, 'public://');
  622. }
  623. else {
  624. $this->error('The Devel Generate module is not enabled, it must be added to the $modules array in the setUp() method for this test class.');
  625. }
  626. }
  627. /**
  628. * Create an image file object of a specific size & type.
  629. *
  630. * @param string
  631. * The size of the requested image in 'XxY' format; defaults to '200x200'.
  632. * @param string
  633. * The image format to use, defaults to 'png'.
  634. *
  635. * @return object
  636. * The file object for the generated image.
  637. */
  638. function generateImageFile($image_size = '200x200', $format = 'png') {
  639. // Generate a test image.
  640. $image_uri = $this->generateImage();
  641. // Create a file object for this image.
  642. $file = new StdClass();
  643. $file->fid = NULL;
  644. $file->uid = 1;
  645. $file->uri = $image_uri;
  646. $file->filemime = file_get_mimetype($image_uri);
  647. $file->filesize = filesize($image_uri);
  648. $file->status = 1;
  649. $file->timestamp = filemtime($image_uri);
  650. $saved_file = file_save($file);
  651. return $saved_file;
  652. }
  653. /**
  654. * Verify a user entity's meta tags load correctly.
  655. *
  656. * @param object $user
  657. * A user object that is to be tested.
  658. */
  659. function assertUserEntityTags($user) {
  660. // Load the user's profile page.
  661. $this->drupalGet('user/' . $user->uid);
  662. $this->assertResponse(200);
  663. // Verify the title is using the custom default for this vocabulary.
  664. $xpath = $this->xpath("//meta[@name='abstract']");
  665. $this->assertEqual(count($xpath), 1, 'Exactly one abstract meta tag found.');
  666. $this->assertEqual($xpath[0]['content'], $user->name . " ponies");
  667. }
  668. /**
  669. * Generate a string that is allowable as a machine name.
  670. *
  671. * @param int $length
  672. * How long the machine name will be, defaults to eight characters.
  673. *
  674. * @return string
  675. * A string that contains lowercase letters and numbers, with a letter as
  676. * the first character.
  677. */
  678. function randomMachineName($length = 8) {
  679. return strtolower($this->randomName($length));
  680. }
  681. }