/** * Generated the entry for the users table. * * @param string $user_login The user login slug * * @return array An associtive array of column/values for the "users" table. */ public static function generateUserTableDataFrom($user_login, array $userData = array()) { $utils = new Slugifier(); $login = $utils->slugify($user_login); $usersTableDefaults = array('user_login' => $login, 'user_pass' => WpPassword::instance()->make($user_login), 'user_nicename' => $user_login, 'user_email' => $login . "@example.com", 'user_url' => "http://{$login}.example.com", 'user_registered' => Date::now(), 'user_activation_key' => '', 'user_status' => '0', 'display_name' => $user_login); if (!empty($userData['user_pass'])) { $userData['user_pass'] = WpPassword::instance()->make($userData['user_pass']); } return array_merge($usersTableDefaults, array_intersect_key($userData, $usersTableDefaults)); }
/** * @test * it should allow having many comments with number placeholder */ public function it_should_allow_having_many_comments_with_number_placeholder(FunctionalTester $I) { $postId = $I->havePostInDatabase(); $overrides = ['comment_author' => 'Luca', 'comment_author_email' => '*****@*****.**', 'comment_author_url' => 'https://theaveragedev.com', 'comment_author_IP' => '111.222.333.444', 'comment_date' => Date::now(), 'comment_date_gmt' => Date::gmtNow(), 'comment_content' => "No comment", 'comment_karma' => '3', 'comment_approved' => '0', 'comment_agent' => 'some agent', 'comment_type' => 'status', 'comment_parent' => 23, 'user_id' => 12]; $ids = $I->haveManyCommentsInDatabase(5, $postId, $overrides); $I->assertEquals(5, count($ids)); for ($i = 0; $i < count($ids); $i++) { foreach ($overrides as $key => $value) { $I->seeInDatabase($I->grabCommentsTableName(), ['comment_post_ID' => $postId, 'comment_ID' => $ids[$i], $key => str_replace('{{n}}', $i, $value)]); } } }
/** * @test * it should allow overriding with n key when having many posts */ public function it_should_allow_overriding_with_n_key_when_having_many_posts(FunctionalTester $I) { $table = $I->grabPostsTableName(); Date::_injectNow(time() - 300); $now = Date::now(); $gmtNow = Date::gmtNow(); $overrides = ['post_author' => '{{n}}', 'post_date' => $now, 'post_date_gmt' => $gmtNow, 'post_content' => "Post content {{n}}", 'post_title' => 'Post title {{n}}', 'post_excerpt' => "Post excerpt {{n}}", 'post_status' => 'draft', 'comment_status' => 'closed', 'ping_status' => 'closed', 'post_password' => '{{n}}foo{{n}}', 'post_name' => 'post-title-{{n}}', 'to_ping' => 1, 'pinged' => 1, 'post_modified' => $now, 'post_modified_gmt' => $gmtNow, 'post_content_filtered' => 'Foo bar {{n}}{{n}}', 'post_parent' => 23, 'guid' => 'http://example.com/?p={{n}}&var={{n}}', 'menu_order' => '{{n}}', 'post_type' => 'antother_type']; $post_ids = $I->haveManyPostsInDatabase(3, $overrides); foreach ($overrides as $key => $value) { for ($i = 0; $i < count($post_ids); $i++) { $post_id = $post_ids[$i]; $processedValue = str_replace('{{n}}', $i, $value); $I->seeInDatabase($table, ['ID' => $post_id, $key => $processedValue]); } } }
protected static function getDefaults($id, $url) { $title = self::generateTitle($id); $defaults = array('post_author' => 1, 'post_date' => Date::now(), 'post_date_gmt' => Date::gmtNow(), 'post_content' => self::generateContent($id), 'post_title' => $title, 'post_excerpt' => self::generateExcerpt($id), 'post_status' => 'publish', 'comment_status' => 'open', 'ping_status' => 'open', 'post_password' => '', 'post_name' => (new Slugifier())->slugify($title), 'to_ping' => '', 'pinged' => '', 'post_modified' => Date::now(), 'post_modified_gmt' => Date::gmtNow(), 'post_content_filtered' => '', 'post_parent' => 0, 'guid' => "{$url}/?p={$id}", 'menu_order' => 0, 'post_type' => 'post'); return $defaults; }
/** * Sets up the required tables for a WordPress multisite installation if not present in the database. * * @param bool $subdomainInstall Whether this is a subdomain multisite installation or a subfolder one. * * @param bool $needHtaccess Whether an `.htaccess` file should be put in place or not. * @param int $sleep A number in seconds the method should wait for db and files operation to complete; def. `0`. * * @return array An array containing exit information about multisite tables created/altered/updated. * @throws ModuleConfigException */ public function haveMultisiteInDatabase($subdomainInstall = true, $needHtaccess = false, $sleep = 0) { $this->isSubdomainMultisiteInstall = $subdomainInstall; $this->needHtaccess = $needHtaccess; $this->isMultisite = true; $dbh = $this->driver->getDbh(); foreach ($this->tables->multisiteTables() as $table) { $operation = 'create'; $prefixedTableName = $this->grabPrefixedTableNameFor($table); if ($this->_seeTableInDatabase($prefixedTableName)) { $query = $this->tables->getAlterTableQuery($table, $this->config['tablePrefix']); $operation = 'alter'; } else { $query = $this->tables->getCreateTableQuery($table, $this->config['tablePrefix']); } if (!empty($query)) { $sth = $dbh->prepare($query); $this->debugSection('Query', $sth->queryString); $out[$table] = ['operation' => $operation, 'exit' => $sth->execute([])]; } else { $out[$table] = ['operation' => $operation, 'exit' => false]; } } $domain = $this->getSiteDomain(); if (!$this->countInDatabase($this->grabSiteTableName(), ['domain' => $domain])) { $this->haveInDatabase($this->grabSiteTableName(), ['domain' => $domain, 'path' => '/']); } if (!$this->countInDatabase($this->grabBlogsTableName(), ['blog_id' => 1])) { $this->query("ALTER TABLE {$this->grabBlogsTableName()} AUTO_INCREMENT=1"); $mainBlogData = ['site_id' => 1, 'domain' => $domain, 'path' => '/', 'registered' => Date::now(), 'last_updated' => Date::now(), 'public' => 1]; $this->haveInDatabase($this->grabBlogsTableName(), $mainBlogData); } if ($this->needHtaccess) { $this->replaceFiles(); } if ($sleep) { sleep($sleep); } return $out; }