|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2014 Facebook, Inc. |
| 4 | + * |
| 5 | + * You are hereby granted a non-exclusive, worldwide, royalty-free license to |
| 6 | + * use, copy, modify, and distribute this software in source code or binary |
| 7 | + * form for use in connection with the web services and APIs provided by |
| 8 | + * Facebook. |
| 9 | + * |
| 10 | + * As with any software that integrates with the Facebook platform, your use |
| 11 | + * of this software is subject to the Facebook Developer Principles and |
| 12 | + * Policies [http://developers.facebook.com/policy/]. This copyright notice |
| 13 | + * shall be included in all copies or substantial portions of the software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 21 | + * DEALINGS IN THE SOFTWARE. |
| 22 | + * |
| 23 | + */ |
| 24 | + |
| 25 | +namespace FacebookAdsTest\Object; |
| 26 | + |
| 27 | +use FacebookAds\Object\AbstractCrudObject; |
| 28 | +use FacebookAds\Object\AdAccount; |
| 29 | +use FacebookAds\Object\AdsPixel; |
| 30 | +use FacebookAds\Object\Business; |
| 31 | +use FacebookAds\Object\Fields\AdsPixelsFields; |
| 32 | + |
| 33 | +class AdsPixelTest extends AbstractCrudObjectTestCase { |
| 34 | + |
| 35 | + /** |
| 36 | + * AdsPixels can be created but only one per account can exist |
| 37 | + */ |
| 38 | + public function testCreate() { |
| 39 | + // make sure there's at least one pixel |
| 40 | + $account = new AdAccount($this->getConfig()->accountId); |
| 41 | + $pixels = $account->getAdsPixels(); |
| 42 | + $pixel = null; |
| 43 | + |
| 44 | + if (!$pixels->count()) { |
| 45 | + $pixel = new AdsPixel(null, $this->getConfig()->accountId); |
| 46 | + $pixel->{AdsPixelsFields::NAME} = 'WCA Pixel'; |
| 47 | + $this->assertCanCreate($pixel); |
| 48 | + } else { |
| 49 | + $pixel = $pixels->current(); |
| 50 | + } |
| 51 | + |
| 52 | + $pixel = new AdsPixel(null, $this->getConfig()->accountId); |
| 53 | + $pixel->{AdsPixelsFields::NAME} = $this->getConfig()->testRunId; |
| 54 | + $this->assertCannotCreate($pixel); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @return AdsPixel |
| 59 | + */ |
| 60 | + protected function getAccountPixel() { |
| 61 | + $cursor = (new AdAccount($this->getConfig()->accountId))->getAdsPixels(); |
| 62 | + $this->assertGreaterThanOrEqual(1, $cursor->count()); |
| 63 | + |
| 64 | + return $cursor->current(); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @depends testCreate |
| 69 | + */ |
| 70 | + public function testCrud() { |
| 71 | + $pixel = $this->getAccountPixel(); |
| 72 | + |
| 73 | + $this->assertCanRead($pixel, array( |
| 74 | + AdsPixelsFields::NAME, |
| 75 | + )); |
| 76 | + |
| 77 | + $name = $pixel->{AdsPixelsFields::NAME}; |
| 78 | + |
| 79 | + $this->assertCanUpdate( |
| 80 | + $pixel, |
| 81 | + array( |
| 82 | + AdsPixelsFields::NAME => $this->getConfig()->testRunId.' updated', |
| 83 | + )); |
| 84 | + |
| 85 | + $pixel->{AdsPixelsFields::NAME} = $name; |
| 86 | + $pixel->update(); |
| 87 | + |
| 88 | + $this->assertCanFetchConnection($pixel, 'getAdAccounts', array(), array( |
| 89 | + 'business' => $this->getConfig()->businessId, |
| 90 | + )); |
| 91 | + $this->assertCanFetchConnection($pixel, 'getAgencies'); |
| 92 | + |
| 93 | + $this->assertCannotDelete($pixel); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * @depends testCreate |
| 98 | + */ |
| 99 | + public function testShareWithAdAccount() { |
| 100 | + $this->skipIf('no_business_manager'); |
| 101 | + |
| 102 | + if (!$this->getConfig()->secondaryBusinessId) { |
| 103 | + $this->markTestSkipped('No secondary business provided in config'); |
| 104 | + } |
| 105 | + |
| 106 | + if (!$this->getConfig()->secondaryAccountId) { |
| 107 | + $this->markTestSkipped('No secondary ad account provided in config'); |
| 108 | + } |
| 109 | + |
| 110 | + $account_id_int = (int) substr($this->getConfig()->secondaryAccountId, 4); |
| 111 | + |
| 112 | + $pixel = $this->getAccountPixel(); |
| 113 | + |
| 114 | + $pixel->sharePixelWithAdAccount( |
| 115 | + $this->getConfig()->secondaryBusinessId, |
| 116 | + $account_id_int); |
| 117 | + |
| 118 | + $pixel->unsharePixelWithAdAccount( |
| 119 | + $this->getConfig()->secondaryBusinessId, |
| 120 | + $account_id_int); |
| 121 | + |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * @depends testCreate |
| 126 | + */ |
| 127 | + public function testShareWithAgency() { |
| 128 | + $this->skipIf('no_business_manager'); |
| 129 | + |
| 130 | + if (!$this->getConfig()->secondaryAccountId) { |
| 131 | + $this->markTestSkipped('No secondary business provided in config'); |
| 132 | + } |
| 133 | + |
| 134 | + $pixel = $this->getAccountPixel(); |
| 135 | + |
| 136 | + $business = new Business($this->getConfig()->businessId); |
| 137 | + $cursor = $business->getAgencies(); |
| 138 | + $cursor->setUseImplicitFetch(true); |
| 139 | + |
| 140 | + $agency_id = null; |
| 141 | + foreach ($cursor as $agency) { |
| 142 | + if ( |
| 143 | + $agency->{AbstractCrudObject::FIELD_ID} |
| 144 | + == $this->getConfig()->secondaryBusinessId |
| 145 | + ) { |
| 146 | + $agency_id = $agency->{AbstractCrudObject::FIELD_ID}; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + if ($agency_id === null) { |
| 151 | + $this->markTestSkipped("Secondary business is not a valid agency"); |
| 152 | + } |
| 153 | + |
| 154 | + $pixel->sharePixelWithAgency( |
| 155 | + $this->getConfig()->businessId, |
| 156 | + $this->getConfig()->secondaryBusinessId); |
| 157 | + |
| 158 | + $pixel->unsharePixelWithAgency( |
| 159 | + $this->getConfig()->businessId, |
| 160 | + $this->getConfig()->secondaryBusinessId); |
| 161 | + } |
| 162 | +} |
0 commit comments