123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- package seo
- import (
- "context"
- "net/http"
- "net/url"
- "testing"
- _ "github.com/lib/pq"
- )
- func TestRender(t *testing.T) {
- u, _ := url.Parse("http://dev.qor5.com/product/1")
- defaultRequest := &http.Request{
- Method: "GET",
- URL: u,
- }
- globlalSeoSetting := TestQorSEOSetting{
- QorSEOSetting: QorSEOSetting{
- Name: GlobalSEO,
- Setting: Setting{
- Title: "global | {{SiteName}}",
- },
- Variables: map[string]string{"SiteName": "Qor5 dev"},
- },
- }
- tests := []struct {
- name string
- prepareDB func()
- collection *Collection
- obj interface{}
- want string
- }{
- {
- name: "Render Golabl SEO with setting variables and default context variables",
- prepareDB: func() { GlobalDB.Save(&globlalSeoSetting) },
- collection: NewCollection().SetSettingModel(&TestQorSEOSetting{}),
- obj: GlobalSEO,
- want: `
- <title>global | Qor5 dev</title>
- <meta property='og:url' name='og:url' content='http://dev.qor5.com/product/1'>
- `,
- },
- {
- name: "Render seo setting with global setting variables",
- prepareDB: func() {
- GlobalDB.Save(&globlalSeoSetting)
- product := TestQorSEOSetting{
- QorSEOSetting: QorSEOSetting{
- Name: "Product",
- Setting: Setting{
- Title: "product | {{SiteName}}",
- },
- },
- }
- GlobalDB.Save(&product)
- },
- collection: NewCollection().SetSettingModel(&TestQorSEOSetting{}).RegisterSEOByNames("Product"),
- obj: "Product",
- want: `<title>product | Qor5 dev</title>`,
- },
- {
- name: "Render seo setting with setting and context variables",
- prepareDB: func() {
- GlobalDB.Save(&globlalSeoSetting)
- product := TestQorSEOSetting{
- QorSEOSetting: QorSEOSetting{
- Name: "Product",
- Setting: Setting{
- Title: "product {{ProductTag}} | {{SiteName}}",
- },
- Variables: map[string]string{"ProductTag": "Men"},
- },
- }
- GlobalDB.Save(&product)
- },
- collection: func() *Collection {
- collection := NewCollection().SetSettingModel(&TestQorSEOSetting{})
- collection.RegisterSEO("Product").
- RegisterSettingVaribles(struct{ ProductTag string }{}).
- RegisterContextVariables("og:image", func(_ interface{}, _ *Setting, _ *http.Request) string {
- return "http://dev.qor5.com/images/logo.png"
- })
- return collection
- }(),
- obj: "Product",
- want: `
- <title>product Men | Qor5 dev</title>
- <meta property='og:image' name='og:image' content='http://dev.qor5.com/images/logo.png'>`,
- },
- {
- name: "Render model setting with global and seo setting variables",
- prepareDB: func() {
- GlobalDB.Save(&globlalSeoSetting)
- product := TestQorSEOSetting{
- QorSEOSetting: QorSEOSetting{
- Name: "Product",
- Variables: map[string]string{"ProductTag": "Men"},
- },
- }
- GlobalDB.Save(&product)
- },
- collection: func() *Collection {
- collection := NewCollection().SetSettingModel(&TestQorSEOSetting{})
- collection.RegisterSEO(&Product{})
- return collection
- }(),
- obj: Product{
- Name: "product 1",
- SEO: Setting{
- Title: "product1 | {{ProductTag}} | {{SiteName}}",
- EnabledCustomize: true,
- },
- },
- want: `<title>product1 | Men | Qor5 dev</title>`,
- },
- {
- name: "Render model setting with default seo setting",
- prepareDB: func() {
- GlobalDB.Save(&globlalSeoSetting)
- product := TestQorSEOSetting{
- QorSEOSetting: QorSEOSetting{
- Name: "Product",
- Setting: Setting{
- Title: "product | Qor5 dev",
- },
- Variables: map[string]string{"ProductTag": "Men"},
- },
- }
- GlobalDB.Save(&product)
- },
- collection: func() *Collection {
- collection := NewCollection().SetSettingModel(&TestQorSEOSetting{})
- collection.RegisterSEO(&Product{})
- return collection
- }(),
- obj: Product{
- Name: "product 1",
- SEO: Setting{
- Title: "product1 | {{ProductTag}} | {{SiteName}}",
- EnabledCustomize: false,
- },
- },
- want: `<title>product | Qor5 dev</title>`,
- },
- {
- name: "Render model setting with inherite gloabl and seo setting",
- prepareDB: func() {
- GlobalDB.Save(&globlalSeoSetting)
- product := TestQorSEOSetting{
- QorSEOSetting: QorSEOSetting{
- Name: "Product",
- Setting: Setting{
- Description: "product description",
- },
- Variables: map[string]string{"ProductTag": "Men"},
- },
- }
- GlobalDB.Save(&product)
- },
- collection: func() *Collection {
- collection := NewCollection().SetSettingModel(&TestQorSEOSetting{})
- collection.RegisterSEO(&Product{})
- return collection
- }(),
- obj: Product{
- Name: "product 1",
- SEO: Setting{
- Keywords: "shoes, {{ProductTag}}",
- EnabledCustomize: true,
- },
- },
- want: `
- <title>global | Qor5 dev</title>
- <meta name='description' content='product description'>
- <meta name='keywords' content='shoes, Men'>
- `,
- },
- {
- name: "Render model setting without inherite gloabl and seo setting",
- prepareDB: func() {
- GlobalDB.Save(&globlalSeoSetting)
- product := TestQorSEOSetting{
- QorSEOSetting: QorSEOSetting{
- Name: "Product",
- Setting: Setting{
- Description: "product description",
- },
- Variables: map[string]string{"ProductTag": "Men"},
- },
- }
- GlobalDB.Save(&product)
- },
- collection: func() *Collection {
- collection := NewCollection().SetInherited(false).SetSettingModel(&TestQorSEOSetting{})
- collection.RegisterSEO(&Product{})
- return collection
- }(),
- obj: Product{
- Name: "product 1",
- SEO: Setting{
- Keywords: "shoes, {{ProductTag}}",
- EnabledCustomize: true,
- },
- },
- want: `
- <title></title>
- <meta name='description'>
- <meta name='keywords' content='shoes, Men'>
- `,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- resetDB()
- tt.prepareDB()
- if got, _ := tt.collection.Render(tt.obj, defaultRequest).MarshalHTML(context.TODO()); !metaEqual(string(got), tt.want) {
- t.Errorf("Render = %v, want %v", string(got), tt.want)
- }
- })
- }
- }
|