PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Tests/DfpTestBase.php

https://gitlab.com/Drulenium-bot/dfp
PHP | 226 lines | 87 code | 24 blank | 115 comment | 4 complexity | a14f72db9ed1bb8595abcdf27f866539 MD5 | raw file
  1. <?php
  2. /**
  3. * @file
  4. * Contains \Drupal\dfp\Tests\DfpTestBase.
  5. */
  6. namespace Drupal\dfp\Tests;
  7. use Drupal\Component\Utility\Unicode;
  8. use Drupal\Dfp\Entity\Tag;
  9. use Drupal\dfp\Entity\TagInterface;
  10. use Drupal\dfp\View\TagView;
  11. use Drupal\simpletest\WebTestBase;
  12. /**
  13. * An abstract class to build DFP tests from.
  14. */
  15. abstract class DfpTestBase extends WebTestBase {
  16. /**
  17. * An admin user.
  18. *
  19. * @var \Drupal\user\Entity\User
  20. */
  21. protected $adminUser;
  22. /**
  23. * Modules to enable.
  24. *
  25. * @var array
  26. */
  27. public static $modules = ['dfp'];
  28. /**
  29. * {@inheritdoc}
  30. */
  31. protected function setUp() {
  32. parent::setUp();
  33. // Create an admin user with all the permissions needed to run tests.
  34. $this->adminUser = $this->drupalCreateUser([
  35. 'administer DFP',
  36. 'access administration pages',
  37. 'administer blocks',
  38. ]);
  39. $this->drupalLogin($this->adminUser);
  40. // Set up global settings needed for DFP ads to work.
  41. \Drupal::configFactory()->getEditable('dfp.settings')
  42. ->set('network_id', '12345')
  43. ->set('default_slug', 'Global DFP slug')
  44. ->save();
  45. }
  46. /**
  47. * Creates a basic dfp ad tag.
  48. *
  49. * @param array $edit
  50. * An array of values for the DFP tag form.
  51. *
  52. * @return \Drupal\dfp\Entity\Tag
  53. * The created DFP tag.
  54. */
  55. protected function dfpCreateTag($edit = []) {
  56. // Create a new tag.
  57. $edit += $this->dfpBasicTagEditValues();
  58. $this->drupalPostForm('admin/structure/dfp/tags/add', $edit, t('Save'));
  59. // Load the tag object.
  60. $tag = Tag::load($edit['id']);
  61. $this->assertTrue(is_object($tag) && $tag->id() == $edit['id'], 'The new DFP tag was saved correctly.');
  62. // Display the new tag.
  63. $this->drupalPlaceBlock('dfp_ad:' . $tag->uuid());
  64. return $tag;
  65. }
  66. /**
  67. * Edits a given tag specified by $id with the given values.
  68. *
  69. * @param string $id
  70. * The DFP tag ID.
  71. * @param array $edit
  72. * An array of values for the DFP tag form.
  73. *
  74. * @return \Drupal\dfp\Entity\Tag
  75. * The edited DFP tag.
  76. */
  77. protected function dfpEditTag($id, &$edit) {
  78. // Make sure there is no machinename set when we are editing.
  79. if (isset($edit['id'])) {
  80. unset($edit['id']);
  81. }
  82. $this->drupalPostForm('admin/structure/dfp/tags/manage/' . $id, $edit, t('Save'));
  83. return Tag::load($id);
  84. }
  85. /**
  86. * Converts a DFP Tag config entity to a TagView object.
  87. *
  88. * @param \Drupal\dfp\Entity\TagInterface $tag
  89. * The DFP tag.
  90. *
  91. * @return \Drupal\dfp\View\TagView
  92. * The TagView object.
  93. */
  94. protected function dfpTagToTagView(TagInterface $tag) {
  95. return new TagView($tag, $this->getGlobalConfig(), $this->container->get('dfp.token'), $this->container->get('module_handler'));
  96. }
  97. /**
  98. * Creates a simple form values $edit array to be used to create a DFP tag.
  99. *
  100. * @return array
  101. * A simple $edit array to be used on the DFP tag form.
  102. */
  103. protected function dfpBasicTagEditValues() {
  104. $machinename = $this->randomMachineName(16);
  105. $basic_tag = [
  106. 'id' => Unicode::strtolower($machinename),
  107. 'slot' => $machinename,
  108. 'size' => implode(',', $this->dfpGenerateSize(2)),
  109. 'adunit' => $this->randomMachineName(),
  110. 'block' => 1,
  111. 'slug' => $this->randomMachineName(32),
  112. 'adsense_backfill[ad_types]' => '',
  113. 'adsense_backfill[channel_ids]' => '',
  114. 'adsense_backfill[color][background]' => '',
  115. 'adsense_backfill[color][border]' => '',
  116. 'adsense_backfill[color][link]' => '',
  117. 'adsense_backfill[color][text]' => '',
  118. 'adsense_backfill[color][url]' => '',
  119. 'targeting[0][target]' => $this->randomMachineName(8),
  120. 'targeting[0][value]' => $this->randomMachineName(8),
  121. 'breakpoints[0][browser_size]' => $this->dfpGenerateSize(),
  122. 'breakpoints[0][ad_sizes]' => implode(',', $this->dfpGenerateSize(2)),
  123. ];
  124. return $basic_tag;
  125. }
  126. /**
  127. * Generates a random size (or array of sizes) to use when testing tags.
  128. *
  129. * @param int $count
  130. * How many sizes to generate.
  131. *
  132. * @return string|array
  133. * A size formatted as ###x### or an array of sizes if $count > 1.
  134. */
  135. protected function dfpGenerateSize($count = 1) {
  136. $sizes = [
  137. '300x250', '300x600', '728x90', '728x10', '160x600', '120x80', '300x100',
  138. '50x50', '160x300',
  139. ];
  140. shuffle($sizes);
  141. return $count == 1 ? array_pop($sizes) : array_slice($sizes, 0, min($count, count($sizes)));
  142. }
  143. /**
  144. * Gets the global DFP settings.
  145. *
  146. * @return \Drupal\Core\Config\ImmutableConfig
  147. * The global DFP settings.
  148. */
  149. protected function getGlobalConfig() {
  150. return \Drupal::config('dfp.settings');
  151. }
  152. /**
  153. * Assert that a property is properly being set.
  154. *
  155. * @param string $property
  156. * The property.
  157. * @param string $key
  158. * The key.
  159. * @param string $val
  160. * The value.
  161. *
  162. * @return bool
  163. * TRUE if the property is set, FALSE otherwise.
  164. */
  165. protected function assertPropertySet($property, $key, $val) {
  166. $pattern = $this->getPropertyPattern($property, $key, $val);
  167. return $this->assertPattern($pattern, 'A ' . $property . ' property was set for ' . $key . ' = ' . $val);
  168. }
  169. /**
  170. * Assert that a property is not being set.
  171. *
  172. * @param string $property
  173. * The property.
  174. * @param string $key
  175. * The key.
  176. * @param string $val
  177. * The value.
  178. *
  179. * @return bool
  180. * TRUE if the property is not set, FALSE otherwise.
  181. */
  182. protected function assertPropertyNotSet($property, $key, $val) {
  183. $pattern = $this->getPropertyPattern($property, $key, $val);
  184. return $this->assertNoPattern($pattern, 'A ' . $property . ' property was not set for ' . $key . ' = ' . $val);
  185. }
  186. /**
  187. * Gets pattern used in assertPropertySet() and assertPropertyNotSet().
  188. *
  189. * @param string $property
  190. * The property.
  191. * @param string $key
  192. * The key.
  193. * @param string $val
  194. * The value.
  195. *
  196. * @return string
  197. * The pattern.
  198. */
  199. private function getPropertyPattern($property, $key, $val) {
  200. return '|' . '.set' . $property . '\(\'' . $key . '\',{1}\s(.)*' . addslashes($val) . '|';
  201. }
  202. }