metatag.helper.test 24 KB

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